如何使用web-api上传文件

时间:2021-11-14 02:13:14

Client side code:

客户端代码:

<form action="api/MyAPI" method="post" enctype="multipart/form-data">     
<label for="somefile">File</label>     <input name="somefile" type="file" />     
<input type="submit" value="Submit" /> 
</form>

And how to process upload file with mvc web-api,have some sample code?

如何使用mvc web-api处理上传文件,有一些示例代码?

2 个解决方案

#1


0  

HTML Code:

HTML代码:

<form action="api/MyAPI" method="post" enctype="multipart/form-data">     
    <label for="somefile">File</label>     
     <input name="somefile" type="file" />     
    <input type="submit" value="Submit" /> 
    </form>

Controller

调节器

         // POST api/MyAPI
        public HttpResponseMessage Post()
        {
            HttpResponseMessage result = null;
            var httpRequest = HttpContext.Current.Request;
            if (httpRequest.Files.AllKeys[0] == "image")
            {
                if (httpRequest.Files.Count > 0)
                {
                    var docfiles = new List<string>();
                    foreach (string file in httpRequest.Files)
                    {
                        var postedFile = httpRequest.Files[file];
                        var filePath = HttpContext.Current.Server.MapPath("~/Images/" + postedFile.FileName);
                        postedFile.SaveAs(filePath);

                        docfiles.Add(filePath);
                    }
                    result = Request.CreateResponse(HttpStatusCode.Created, docfiles);


                }
            }
            else
            {
                result = Request.CreateResponse(HttpStatusCode.BadRequest);
            }
            return result;
        }

try below link

尝试以下链接

this link use for me hopefully it will work you

这个链接对我有用,希望它对你有用

http://www.asp.net/web-api/overview/advanced/sending-html-form-data,-part-2

http://www.asp.net/web-api/overview/advanced/sending-html-form-data,-part-2

#2


0  

You can use ApiMultipartFormFormmatter to upload file to web api 2. By using this library, you can define a view model to get parameters submitted from client-side. Such as:

您可以使用ApiMultipartFormFormmatter将文件上载到web api 2.通过使用此库,您可以定义视图模型以获取从客户端提交的参数。如:

public class UploadFileViewModel 
{
    public HttpFile Somefile{get;set;}
}

And use it in your Api controller like this:

并在你的Api控制器中使用它,如下所示:

public IHttpActionResult Upload(UploadFileViewModel info)
{
    if (info == null)
    {
        info = new UploadFileViewModel();
        Validate(info);
    }

    if (!ModelState.IsValid)
        return BadRequest(ModelState);

    return Ok();
}

Nested objects can be parsed by this library.

此库可以解析嵌套对象。

#1


0  

HTML Code:

HTML代码:

<form action="api/MyAPI" method="post" enctype="multipart/form-data">     
    <label for="somefile">File</label>     
     <input name="somefile" type="file" />     
    <input type="submit" value="Submit" /> 
    </form>

Controller

调节器

         // POST api/MyAPI
        public HttpResponseMessage Post()
        {
            HttpResponseMessage result = null;
            var httpRequest = HttpContext.Current.Request;
            if (httpRequest.Files.AllKeys[0] == "image")
            {
                if (httpRequest.Files.Count > 0)
                {
                    var docfiles = new List<string>();
                    foreach (string file in httpRequest.Files)
                    {
                        var postedFile = httpRequest.Files[file];
                        var filePath = HttpContext.Current.Server.MapPath("~/Images/" + postedFile.FileName);
                        postedFile.SaveAs(filePath);

                        docfiles.Add(filePath);
                    }
                    result = Request.CreateResponse(HttpStatusCode.Created, docfiles);


                }
            }
            else
            {
                result = Request.CreateResponse(HttpStatusCode.BadRequest);
            }
            return result;
        }

try below link

尝试以下链接

this link use for me hopefully it will work you

这个链接对我有用,希望它对你有用

http://www.asp.net/web-api/overview/advanced/sending-html-form-data,-part-2

http://www.asp.net/web-api/overview/advanced/sending-html-form-data,-part-2

#2


0  

You can use ApiMultipartFormFormmatter to upload file to web api 2. By using this library, you can define a view model to get parameters submitted from client-side. Such as:

您可以使用ApiMultipartFormFormmatter将文件上载到web api 2.通过使用此库,您可以定义视图模型以获取从客户端提交的参数。如:

public class UploadFileViewModel 
{
    public HttpFile Somefile{get;set;}
}

And use it in your Api controller like this:

并在你的Api控制器中使用它,如下所示:

public IHttpActionResult Upload(UploadFileViewModel info)
{
    if (info == null)
    {
        info = new UploadFileViewModel();
        Validate(info);
    }

    if (!ModelState.IsValid)
        return BadRequest(ModelState);

    return Ok();
}

Nested objects can be parsed by this library.

此库可以解析嵌套对象。