提出问题提出问题
 

建议的答复WCF - File Uploading

  • 2008年9月11日 7:39jd12215 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    I have been trying to create a WCF web service that would allow uploading different files to the web server. There doesn't seem to be much out there with regards to file uploading and WCF but I did manage to find ueira post on "receiving arbitrary data" and went ahead and implement the custom binding to accept RAW. This worked, but still got problems.

    I can upload a file (png, pdf, txt, whatever), write the stream using FileStream.write() but when I try to open it there are errors. Images and PDF's don't show and txt files always show the multipart/form-data boundaries.

    My questions are:

     - Why don't examples like Kjells work with binary data (content-type => application/octet-stream)?
     - When using Carlos' custom binding, why are the boundaries being added to the file when written to disk? Am I not implementing the file save correctly?

    I've seen some examples that use Byte arrays instead of Streams but Microsofts articles seem to suggest that streaming is the way to go.

     SaveFile():
    Code Snippet

           public void SaveFile(string fileName, Stream logFile)
            {
                if (!uploadDirectory.EndsWith("\\"))
                    uploadDirectory += "\\";

                WebOperationContext ctx = WebOperationContext.Current;


                try
                {
                    FileStream fs = null;
                    /* USE STREAMING */

                    using (fs = new FileStream(uploadDirectory + fileName, FileMode.Create, FileAccess.Write, FileShare.None))
                    {
                        const int bufferLen = 1024; //read stream @ 4K chunks
                        byte[] buffer = new byte[bufferLen];

                        int bytesRead = 0;
                        while ((bytesRead = logFile.Read(buffer, 0, bufferLen)) > 0)
                        {
                            fs.Write(buffer, 0, bytesRead);
                            ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.Continue;
                        }

                        fs.Close();
                        logFile.Close();

                        ctx.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;
                        return;
                    }
                }
                catch (IOException ex)
                {
                    throw ex;
                }
            }


全部回复

  • 2008年9月11日 11:44Will.Rogers 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Here's the sample I've based my solution on.

    http://www.codeproject.com/KB/WCF/WCF_FileTransfer_Progress.aspx

    It would be really nice if the community could get some prescriptive guidance for this common task of uploading and downloading files over HTTP from the Microsoft WCF team.

    Thanks,
    Will.
  • 2008年9月11日 22:48jd12215 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Thanks for the link.

    One question that comes to mind: What binding are you using on your endpoint (e.g. basicHttpBinding) ?

    How is your web.config configured?

    Thanks
    Jose
  • 2008年9月12日 1:32Will.Rogers 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    I've got it working with both wsHTTPBinding and basicHttpBinding. My web.config just adds all the WCF stuff and increases the maxReqeustLength like so:

    <system.web>
    <httpRuntime maxRequestLength="8000"

  • 2008年9月12日 6:19jd12215 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     建议的答复
    This is what I don't understand and maybe has to do with .NETfx 3.5 SP1 or something but I implemented your code in a simple service and I still get (in the tracer log)

    Content Type multipart/form-data; boundary=---------------------------13707388261950367213631361083 was sent to a service expecting multipart/related; type="application/xop+xml".  The client and service bindings may be mismatched.

    I know that the html upload form that I created sends with enctype = multipart/form-data and that it also goes up with a content type of application/octet-stream. Apparently this is causing a problem with the service.

    Has anyone had this issue before?

    I don't have this problem with Carlos' "raw data" code, but I still can't get the files to open correctly once on the server.
  • 2008年9月22日 19:11KjellSJ 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    How are you hosting your servce ? IIS6, IIS7 integrated or classic mode, WAS, self, console, or just the VS built-in web server ?

     

  • 2008年9月22日 19:15jd12215 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    The service is hosted on IIS 5.1 (Win XP Pro)