How to Upload Image Via WebApi

前端 未结 6 1012
忘了有多久
忘了有多久 2020-12-02 12:53

How I can upload Image File via ASP.NET Web API?
I have an input tag in File mode and it posted to API, how I can save it to server folder?
I tried th

6条回答
  •  自闭症患者
    2020-12-02 13:05

    If your API only allows one image at a time, System.Web.Helpers.WebImage may help. Just make sure that a filename is included.

    ------WebKitFormBoundaryzrmNUJnUirtKajVF Content-Disposition: form-data; name="image"; filename="IMG_3321.JPG" Content-Type: image/jpeg

    ------WebKitFormBoundaryzrmNUJnUirtKajVF--

    [HttpPost]
    [ResponseType(typeof(Models.Photo))]
    [Route("upload")]
    public async Task Upload()
    {
        var img = WebImage.GetImageFromRequest();
        if (img == null)
        {
            return BadRequest("Image is null.");
        }
    
        // Do whatever you want with the image (resize, save, ...)
    
        // In this case, I save the image to a cloud storage and
        // create a DB record to reference the image.
    }
    

提交回复
热议问题