将文件从客户端流传输到webrequest

| 我正在从客户端计算机到远程云主机的c#Web应用程序中上传文件。用户输入文件名和位置,然后将其传递给服务器。我正在将文件内容分块传输到请求。但是,文件到达目的地时无效。我确定它与我的请求标头和垃圾类似,因为从服务器上传到远程位置后,我能够使它工作。任何人都可以发现问题所在吗?
while (bytesRemaining > 0)
{
    if (!sw.CanRead)
    {
        sw = File.OpenRead(txtFileUpload.Text);
        if (offset > 0)
            sw.Seek(offset, SeekOrigin.Begin);
    }

    int count = sw.Read(buffer, 0, (int) chunk);

    request =
        (HttpWebRequest)
        WebRequest.Create(\"http://xxxxxx.com/write/\" +
                          offset);
    request.Method = \"POST\";
    request.ReadWriteTimeout = int.MaxValue;
    request.Timeout = int.MaxValue;
    request.KeepAlive = false;
    string boundary = \"----------------------------\" + DateTime.Now.Ticks.ToString(\"x\");
    request.ContentType = \"multipart/form-data; boundary=\" + boundary;

    var postData = new MemoryStream();
    const string newLine = \"\\r\\n\";
    var sw3 = new StreamWriter(postData);
    sw3.Write(\"--\" + boundary + newLine);
    sw3.Write(\"Content-Disposition: form-data;name=\\\"{0}\\\";filename=\\\"{1}\\\"{2}\", \"upload\",
              txtFileName.Text, newLine);
    sw3.Write(\"Content-Type: multipart/form-data \" + newLine + newLine);
    sw3.Flush();

    postData.Write(buffer, 0, count);
    sw3.Write(newLine);
    sw3.Write(\"--{0}--{1}\", boundary, newLine);
    sw3.Flush();

    request.ContentLength = postData.Length;
    using (Stream s = request.GetRequestStream())
        postData.WriteTo(s);

    sw3.Write(\"--\" + boundary + newLine);
    sw3.Write(\"Content-Disposition: form-data;name=\\\"\\\"\" + newLine);
    sw3.Write(\"content-type:octet-stream;charset=windows-1250\" + newLine);

    sw3.Write(\"VALUE\" + newLine);
    sw3.Write(\"--{0}--{1}\", boundary, newLine);
    sw3.Flush();
    postData.Close();

    // These next 3 lines are what I had before, instead of all the previous
    // code, which worked when uploading a file from the server to the remote
    // location.
    //using (Stream sw2 = request.GetRequestStream())
    //{
    //    sw2.Write(buffer, 0, count);
    //}

    using (WebResponse resp = request.GetResponse())
    {
        resp.Close();
    }

    offset += count;
    bytesRemaining -= count;

}
    
已邀请:
        为什么要重新发明轮子?采用
WebClient.UploadFile
http://msdn.microsoft.com/zh-cn/library/system.net.webclient.uploadfile(v=vs.71).aspx 他们甚至有一个例子。     

要回复问题请先登录注册