【问题标题】:File uploading not working on Win phone 8Win phone 8 无法上传文件
【发布时间】:2014-08-09 10:54:30
【问题描述】:

我有一个用户可以在其中录制视频的应用程序和一个用于将录制的视频上传到 azure 的 api。 我正在使用下面的代码

HttpClientHandler handler = new HttpClientHandler();
handler.MaxRequestContentBufferSize = int.MaxValue;
using (HttpClient httpClient = new HttpClient(handler) 
       { 
            MaxResponseContentBufferSize = int.MaxValue
       })
{
    MultipartFormDataContent content = new MultipartFormDataContent();
    content.Add(new ByteArrayContent(chunks), "file", fileName);
    HttpResponseMessage response = null;
    response = await httpClient.PostAsync(new Uri(url), content);
    return await response.Content.ReadAsStringAsync();
}

这仅在视频少于 10 秒时有效。当我尝试上传大小约为 20-30 MB 的视频时,它失败了。 作为回应,我得到了状态码 404。

我还尝试了另一种上传方式。但结果是同样的错误:

string boundary = Guid.NewGuid().ToString();

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "POST";
        request.ContentType = "multipart/form-data;boundary=" + boundary;

        StringBuilder header = new StringBuilder();
        header.Append("\r\n--");
        header.Append(boundary);
        header.Append("\r\n");
        header.Append("Content-Disposition: file; name=\"file\"; filename=\"" + fileName + "\"\r\n");
        header.Append("Content-Type: application/octet-stream\r\n\r\n");
        byte[] headerbytes = Encoding.UTF8.GetBytes(header.ToString());

        StringBuilder footer = new StringBuilder();
        footer.Append("\r\n--");
        footer.Append(boundary);
        footer.Append("--\r\n");
        byte[] footerbytes = Encoding.UTF8.GetBytes(footer.ToString());

        using (var streamWriter = await request.GetRequestStreamAsync())
        {
            streamWriter.Write(headerbytes, 0, headerbytes.Length);              
            streamWriter.Write(footerbytes, 0, footerbytes.Length);
            streamWriter.Flush();
        }

        var httpResponse = (HttpWebResponse)await request.GetResponseAsync();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            return streamReader.ReadToEnd();                
        }

【问题讨论】:

    标签: c# windows-phone-8 upload dotnet-httpclient


    【解决方案1】:

    检查您的 web.api 站点的 web.config,尤其是本节

    <configuration>
      <system.web>
        <httpRuntime maxRequestLength="xxx" />
      </system.web>
    </configuration>
    

    xxx 以 KB 为单位。

    【讨论】:

    • 我刚刚从 api 开发人员那里确认了这一点,他说最大长度限制为 60 MB。但就我而言,即使是 30 MB,我也会遇到错误。
    • @mayank.karki,您能否在 WP 和 iOS/Android 上使用 Burp/fiddler 捕获请求并发布标头进行比较?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-23
    • 2021-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多