locked
WebApi can not get files RRS feed

  • Question

  • User-1556121948 posted

    Hello friends,
     
    I need to develop WEBAPI which gets file / files.
    The checks I make through the postman, attach a file name and sender
    (Can not upload pictures right now I'll go up a little bit).
     
    The problem that is lived in httpRequest.Files.Count does not come from files.
    This means that the file does not arrive.
     
    Here is my code I would be happy to help.
    Also, if something is wrong, I will be happy to have a new code or code.

    public async Task<HttpResponseMessage> PostSendFiles([FromBody]Object jsonData)
            {
                string serviceName = "GetFormData";
                Logs.Info(String.Format("------ Start {0} ---------", serviceName));
                HttpResponseMessage res = new HttpResponseMessage();
                try
                {
                    HttpResponseMessage result = null;
                    var httpRequest = HttpContext.Current.Request;
                    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("~/" + postedFile.FileName);
                            postedFile.SaveAs(filePath);
    
                            docfiles.Add(filePath);
                        }
                        result = Request.CreateResponse(HttpStatusCode.Created, docfiles);
                    }
                    else
                    {
                        result = Request.CreateResponse(HttpStatusCode.BadRequest);
                    }
                    return result;
    
                }
                catch (Exception ex)
                {
                    Logs.Error("Exception", ex);
                    return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
                    throw;
                }
                finally
                {
                    Logs.Info(String.Format("------ END {0} ---------", serviceName));
                }
            }



     
    Thank you

    Sunday, June 18, 2017 6:09 AM

All replies

  • User1967761114 posted

    Hi Daledale,

    According to your description, I suggest you could remove the argument on Web API action,

    see the following code which works well, you could refer to it:

    (1)  html

    <form action="http://localhost:61072/api/test/PostSendFiles" method="post" enctype="multipart/form-data">
        <fieldset>
            <legend> uploader1 </legend>
            <input type="file" name="file1" />
        </fieldset>
        <fieldset>
            <legend> uploader2 </legend>
            <input type="file" name="file2" />
        </fieldset>
        <fieldset>
            <legend> uploader3 </legend>
            <input type="file" name="file3" />
        </fieldset>
        <input type="submit" value="Upload" />
    </form>

    (2) Web API

    [System.Web.Http.HttpPost]
    public HttpResponseMessage PostSendFiles()
    {
        string serviceName = "GetFormData";
        Logs.Info(String.Format("------ Start {0} ---------", serviceName));
        HttpResponseMessage res = new HttpResponseMessage();
        try
        {
            HttpResponseMessage result = null;
            var httpRequest = HttpContext.Current.Request;
            if (httpRequest.Files.Count > 0)
            {
                var docfiles = new List<string>();
                foreach (string file in httpRequest.Files)
                {
                    var postedFile = httpRequest.Files[file];
                    if (postedFile.ContentLength == 0)
                    {
                        continue;
                    }
                    var filePath = HttpContext.Current.Server.MapPath("~/" + postedFile.FileName);
                    postedFile.SaveAs(filePath);
                    docfiles.Add(filePath);
                }
                result = Request.CreateResponse(HttpStatusCode.Created, docfiles);
            }
            else
            {
                result = Request.CreateResponse(HttpStatusCode.BadRequest);
            }
            return result;
    
        }
        catch (Exception ex)
        {
            Logs.Error("Exception", ex);
            return Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
            throw;
        }
        finally
        {
            Logs.Info(String.Format("------ END {0} ---------", serviceName));
        }
    }

     

    If you have any other questions, please feel free to contact me any time.

    Best Regards

    Even

    Monday, June 19, 2017 2:02 AM