我正在尝试使用 .Net Framework 4.5和Web API将文件上传到使用Spring MVC来处理文件上传的第三方客户端。每次尝试都会遇到错误,“不存在必需的MultipartFile参数'文件'。”
还有其他人遇到过这个问题吗?如果是这样你是如何解决的?似乎Web API没有提供适当的机制/容器来发送给Spring,以便它能够识别它。
这是当前的代码。
Uri webService = new Uri(objectInstance);
var fileContent = new ByteArrayContent(System.IO.File.ReadAllBytes(HttpContext.Current.Server.MapPath("taleotest.xml")));//new ByteArrayContent(new byte[100]);
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("file")
{
FileName = @"C:\taleotest.xml"
};
var formData = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("name", "test"),
new KeyValuePair<string, string>("title", "test2")
});
//fileContent.add
var cookieContainer = new CookieContainer();
cookieContainer.Add(webService, new Cookie("authToken", _authToken));
var handler = new HttpClientHandler() { CookieContainer = cookieContainer };
HttpClient httpClient = new HttpClient(handler);
MultipartContent content = new MultipartContent();
content.Add(formData);
content.Add(fileContent);
var response = httpClient.PostAsync(webService, content).Result;
答案 0 :(得分:0)
您是否尝试将CommonsMultipartResolver
属性值添加到applicationContext.xml
文件中? http://forum.springsource.org/showthread.php?66240-Problems-with-MultipartFile-Upload
您能确保所有依赖项都已正确引用吗?一个示例:IE9 issue - Required MultipartFile[] parameter is not present with Jquery-uploadify
可能需要更多信息和代码示例来提供更多帮助,这可能是由许多事情引起的。
答案 1 :(得分:0)
将以下内容添加到我的请求内容中就可以了。
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); // Encoding
byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");