How to upload a file and parameter to a remote server using the HttpClient PostAsync method?

I am trying to upload a file from my desktop application to a remote server. After looking at SO for some time, this approach seems to be the cleanest way around this. The problem is that not a single parameter was received on the server side. What am I missing?

    private void AddFile(FileInfo fileInfo, int folderId)
    {
        using (var handler = new HttpClientHandler() {CookieContainer = _cookies})
        {
            using (var client = new HttpClient(handler) {BaseAddress = new Uri(_host)})
            {
                var requestContent = new MultipartFormDataContent();
                var fileContent = new StreamContent(fileInfo.Open(FileMode.Open));
                var folderContent = new StringContent(folderId.ToString(CultureInfo.InvariantCulture));
                requestContent.Add(fileContent, "file", "file");
                requestContent.Add(folderContent, "folderId", "folderId");

                client.PostAsync("/Company/AddFile", requestContent);
            }
        }
    }

edit: This is the signature that the server side expects:

    [HttpPost]
    public ActionResult AddFile(HttpPostedFileBase file, int folderId)
+4
source share
2 answers

After a lot of trial and error, I got it. There were a few problems. 1) paramater names are expected in quotation marks 2) I was missing a bunch of header information. Here is the working code.

    private void AddFile(FileInfo fileInfo, int folderId)
    {
        using (var handler = new HttpClientHandler() {CookieContainer = _cookies})
        {
            using (var client = new HttpClient(handler) {BaseAddress = new Uri(_host)})
            {
                var requestContent = new MultipartFormDataContent();
                var fileContent = new StreamContent(fileInfo.OpenRead());
                fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                    {
                        Name = "\"file\"",
                        FileName = "\"" + fileInfo.Name + "\""
                    };
                fileContent.Headers.ContentType =
                    MediaTypeHeaderValue.Parse(MimeMapping.GetMimeMapping(fileInfo.Name));
                var folderContent = new StringContent(folderId.ToString(CultureInfo.InvariantCulture));

                requestContent.Add(fileContent);
                requestContent.Add(folderContent, "\"folderId\"");

                var result = client.PostAsync("Company/AddFile", requestContent).Result;
            }
        }
+6
source

: using (var formData = new MultipartFormDataContent()) {formData.Add(new StreamContent (new MemoryStream (bytes)), "", Model.BaseFileName);

: var httpRequest = System.Web.HttpContext.Current.Request;

            if (httpRequest.Files.Count > 0)
            {
                var docfiles = new List<string>();
                //foreach (string file in httpRequest.Files)
                //{
                HttpPostedFile postedFile = httpRequest.Files[0];
                // Initialize the stream.
                Stream mstream = postedFile.InputStream;

                byte[] byteArray = new byte[postedFile.ContentLength];
                postedFile.InputStream.Read(byteArray, 0, postedFile.ContentLength);
0

All Articles