c# - HttpClient StreamContent 附加文件名两次

标签 c# windows-phone-8 content-type image-uploading multipartform-data

我正在使用 Microsoft Http 客户端库从 Windows Phone 8 向服务器发出多部分请求。它包含一个具有 json 字符串的字符串内容和一个具有图像流的流内容。 现在我得到状态 OK 并在服务器上请求命中。但日志显示服务器无法获取图像的文件名。

content.Add(new StreamContent(photoStream), "files", fileName);

其中 photoStream 是图像流,“files”是内容的名称,file name 是图像文件的名称。

因此 header 值必须是:

Content-Disposition: form-data; name=files; filename=image123.jpg

但实际上是:

Content-Disposition: form-data; name=files; filename=image123.jpg; filename*=utf-8''image123.jpg

为什么要附加“; filename*=utf-8''image123.jpg”部分。 有问题吗?

请告诉我无法从 WP8 上传图片的任何原因/可能性。

最佳答案

using (var content = new MultipartFormDataContent())
{
    content.Add(CreateFileContent(imageStream, fileName, "image/jpeg"));
}

private StreamContent CreateFileContent(Stream stream, string fileName, string contentType)
{
    var fileContent = new StreamContent(stream);
    fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") 
    { 
        Name = "\"files\"", 
        FileName = "\"" + fileName + "\""
    };
    fileContent.Headers.ContentType = new MediaTypeHeaderValue(contentType);            
    return fileContent;
}

关于c# - HttpClient StreamContent 附加文件名两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20395455/

相关文章:

c# - 暂停执行直到批处理文件完成

c# - 使用 JSON.Net 序列化包含撇号的字符串

c# - 我们如何在 Windows Phone 8 中将相机作为背景 View 运行?

c# - LINQ to entities - 构建 where 子句以测试多对多关系中的集合

c# - 在区域之间共享代码 ASP.net MVC

email - 从 mbox 文件中提取电子邮件正文,无论字符集和内容传输编码如何,将其解码为纯文本

jquery - 根据 jquery ajax 返回的内容类型不同的操作

python - Content-Type in 用于 python 请求中的单个文件

windows-phone-8 - Windows Phone 8 中的消息提醒

windows-phone-8 - Windows Phone 8 的内存使用限制