Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法使用Web API将文件上载到Spring MVC_C#_Spring Mvc_Asp.net Web Api - Fatal编程技术网

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

C# 无法使用Web API将文件上载到Spring MVC,c#,spring-mvc,asp.net-web-api,C#,Spring Mvc,Asp.net Web Api,我正在尝试使用.NetFramework 4.5和Web API将文件上载到使用Spring MVC处理文件上载的第三方客户端。每次尝试都会遇到错误,“所需的MultipartFile参数'file'不存在。” 还有其他人遇到过这个问题吗?如果是,你是如何解决的?Web API似乎没有提供适当的机制/容器来发送给Spring,以便它能够识别它 这是当前的代码 Uri webService = new Uri(objectInstance); var fileConte

我正在尝试使用.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;
uriwebservice=newuri(objectInstance);
var fileContent=newbytearraycontent(System.IO.File.ReadAllBytes(HttpContext.Current.Server.MapPath(“taleotest.xml”))//新的ByteArrayContent(新字节[100]);
fileContent.Headers.ContentDisposition=新的ContentDispositionHeaderValue(“文件”)
{
FileName=@“C:\taleotest.xml”
};
var formData=new FormUrlEncodedContent(new[]
{
新的KeyValuePair(“名称”、“测试”),
新的KeyValuePair(“标题”、“测试2”)
});
//fileContent.add
var cookieContainer=新的cookieContainer();
添加(webService,新Cookie(“authToken”,“authToken”);
var handler=new-HttpClientHandler(){CookieContainer=CookieContainer};
HttpClient HttpClient=新的HttpClient(处理程序);
MultipartContent内容=新的MultipartContent();
content.Add(formData);
content.Add(fileContent);
var response=httpClient.PostAsync(webService,content).Result;

您是否尝试将
CommonsMultipartResolver
属性值添加到
applicationContext.xml
文件中

您能确保所有依赖项都被正确引用吗?其中一个例子:


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

将以下内容添加到我请求的内容中就成功了

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