将文件从客户端流式传输到 Web 请求

发布于 2024-11-10 00:50:35 字数 2221 浏览 1 评论 0原文

我正在从 ac# 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;

}

I'm working on a file upload from a c# web app, going from the client machine to a remote cloud host. The user types in the file name and location and passes that to the server. I am streaming the contents of the file in chunks to the request. However, the file is not valid when it gets to the destination. I'm sure it's somehow related to my request headers and junk like that, since I'm able to make this work when uploading from the server to the remote location. Can anyone spot what's wrong?

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;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

要走干脆点 2024-11-17 00:50:35

为什么要重新发明轮子?使用

WebClient.UploadFile

http://msdn .microsoft.com/en-us/library/system.net.webclient.uploadfile(v=vs.71).aspx

他们甚至有一个示例。

Why reinvent the wheel ? Use

WebClient.UploadFile

http://msdn.microsoft.com/en-us/library/system.net.webclient.uploadfile(v=vs.71).aspx

They even have an example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文