How To Accept a File POST

前端 未结 13 1628
深忆病人
深忆病人 2020-11-22 09:48

I\'m using asp.net mvc 4 webapi beta to build a rest service. I need to be able to accept POSTed images/files from client applications. Is this possible using the webapi?

13条回答
  •  一个人的身影
    2020-11-22 10:06

    API Controller :

    [HttpPost]
    public HttpResponseMessage Post()
    {
        var httpRequest = System.Web.HttpContext.Current.Request;
    
        if (System.Web.HttpContext.Current.Request.Files.Count < 1)
        {
            //TODO
        }
        else
        {
    
        try
        { 
            foreach (string file in httpRequest.Files)
            { 
                var postedFile = httpRequest.Files[file];
                BinaryReader binReader = new BinaryReader(postedFile.InputStream);
                byte[] byteArray = binReader.ReadBytes(postedFile.ContentLength);
    
            }
    
        }
        catch (System.Exception e)
        {
            //TODO
        }
    
        return Request.CreateResponse(HttpStatusCode.Created);
    }
    

提交回复
热议问题