HttpClient StreamContent追加文件名两次 [英] HttpClient StreamContent append filename twice

查看:180
本文介绍了HttpClient StreamContent追加文件名两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using Microsoft Http Client Libraries to make a multipart request from Windows Phone 8 to the server. It contains a String content having json string and a Stream Content having image stream. Now i get the status OK and request hits on the server. but the logs says the server is not able to get the file name of the image.

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

其中photoStream是图像流,文件"是内容的名称,文件名是图像文件的名称.

where the photoStream is the image stream, "files" is the name of content and file name is the name of the image file.

因此标头值必须为:

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"部分. 有问题吗?

Why it is appending the "; filename*=utf-8''image123.jpg" part. Is it an issue?

请让我知道我无法从WP8上传图像的任何原因/可能性.

Please let me know any reasons/possibility that i am not able to upload image from 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;
}

这篇关于HttpClient StreamContent追加文件名两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆