简体   繁体   中英

Upload Image to URI using HttpClient

I´ma newbie about http related things, so I am a little stacked.

What I want is simple, I need to POST, upload a imaged to a server like this: http://web.com/imageGalerry .

I think its not too much complicated but I dont know why I am no getting errors and so far now I´m not sure how to continue(because the image is not upload in fact), this is my code:

public async Task<object> UpdateGalleryResources(IFormFile file, int idResouce)
        {
            byte[] data;
            string result = "";
            ByteArrayContent bytes;
            var urlToPost = "http://hello.uk/imagegallery/resources/" + 00+ "/" + file.FileName;

            MultipartFormDataContent multiForm = new MultipartFormDataContent();

            try
            {
                using (var client = new HttpClient())
                {
                    using (var br = new BinaryReader(file.OpenReadStream()))
                    {
                        data = br.ReadBytes((int)file.OpenReadStream().Length);
                    }

                    bytes = new ByteArrayContent(data);
                    multiForm.Add(bytes, "file", file.FileName);
                    //multiForm.Add(new StringContent("value1"), "key1");
                    //multiForm.Add(new StringContent("value2"), "key2");


                    var res = await client.PostAsync(urlToPost, multiForm);
                    return res;
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.ToString());
            }
        }

This is the view:

<form action="/Galley/UpdateGallery"  method="post" class="dropzone" id="myDropzone">

    <input type="hidden" value="1" name="idResource" />

</form>

and the dropzone js I am using to handle the view:

document.addEventListener("DOMContentLoaded", function () {

    // access Dropzone here
    //dropzone.js detecta la version 'camelized' cuando el div tiene la clase dropzone
    Dropzone.options.myDropzone = {
        addRemoveLinks: true,
        //autoProcessQueue: false,
.....
       }

And this is the error code I get from return res

  {StatusCode: 404, ReasonPhrase: 'Not Found', Version: 1.1, Content: System.Net.Http.HttpConnection+HttpConnectionResponseContent, Headers:
{
  Server: Microsoft-IIS/8.5
  X-Powered-By: ASP.NET
  Date: Thu, 23 Apr 2020 08:22:52 GMT
  Content-Type: text/html
  Content-Length: 1282
}}

This is what I check in debug mode, everything I think looks right: 在此处输入图像描述

Can you help me about what I am doing wrong?? Thank you.

In the past I've found the following two pieces of code important when uploading images to a ASP.Net controller as an IFormFile

On the form tag in your view add the enctype attribute

<form enctype="multipart/form-data" othertags="..."></form>

For an explanation of what multipart/form-data means: What does enctype='multipart/form-data' mean?

Then add the right binding to your controller parameters

public async Task<object> UpdateGalleryResources([FromForm]IFormFile file, int idResouce)

I'm not sure how this will work with dropzone, but if the IFormFile is appearing as null in the controller this may be worth a try

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM