无法使用Web API将文件上载到Spring MVC

本文关键字:上载 文件 Spring MVC API Web | 更新日期: 2023-09-27 18:26:50

我正在尝试使用.NetFramework 4.5和Web API将文件上载到使用Spring MVC处理文件上载的第三方客户端。每次尝试都会遇到错误,"所需的MultipartFile参数'file'不存在。"

其他人遇到过这个问题吗?如果是,您是如何解决的?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;

无法使用Web API将文件上载到Spring MVC

是否尝试将CommonsMultipartResolver属性值添加到applicationContext.xml文件中?http://forum.springsource.org/showthread.php?66240-多部分文件上传的问题

你能确保所有依赖项都被正确引用吗?示例:IE9问题-Jquery uploadify 不存在所需的MultipartFile[]参数

可能需要更多的信息和代码示例来提供更多帮助,这可能是由许多因素造成的。

在我的请求内容中添加以下内容就成功了。

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");