【问题标题】:Unable to Upload File to Spring MVC using Web API无法使用 Web API 将文件上传到 Spring MVC
【发布时间】:2013-08-10 02:37:17
【问题描述】:

我正在尝试使用 .Net Framework 4.5 和 Web API 将文件上传到使用 Spring MVC 处理文件上传的第 3 方客户端。每次尝试都会遇到错误,“所需的 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;

【问题讨论】:

    标签: c# spring-mvc asp.net-web-api


    【解决方案1】:

    您是否尝试将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

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

    【讨论】:

      【解决方案2】:

      将以下内容添加到我的请求内容中就可以了。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-10-10
        • 2015-11-09
        • 2020-10-17
        • 1970-01-01
        • 2017-08-28
        • 2015-05-20
        • 1970-01-01
        相关资源
        最近更新 更多