0

I´m a 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: enter image description here

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

Qiqke
  • 486
  • 5
  • 19
  • 1
    So are you sure that the request you're posting to the imageGallery server is actually valid? It wouldn't throw an exception if it wasn't, you'll just be getting an error status code in the result. Also you're always returning result, which is just an empty string. If you want to return the result from your POST request you should return the variable you've called res – sunero4 Apr 23 '20 at 08:11
  • 1
    Can you add the exception that is getting thrown and the front end code? It potentially could be a missing binding attribute from the controller – Sam Apr 23 '20 at 08:22
  • hi @sunero4 , I changed that and it still gives a 200, as you can see in the gif in chrome console the `requestUrl` is `http:localhost:port/path` , is this ok? – Qiqke Apr 23 '20 at 08:24
  • Hi sam I´m not getting any exception, and my front code is added – Qiqke Apr 23 '20 at 08:24
  • I have added the error code now – Qiqke Apr 23 '20 at 08:29
  • 1
    Hmm so if you look in the chrome dev console, what is the response body of the UpdateGalleryResources request? – sunero4 Apr 23 '20 at 08:29
  • I have added the response console – Qiqke Apr 23 '20 at 08:30
  • 1
    Okay so you're getting a 404, which typically means that the url that you're sending your request to doesn't exist. When I try to visit the site at hello.uk, it seems like it's actually closed down though, so I'm not sure whether that could be the issue? – sunero4 Apr 23 '20 at 08:34
  • 1
    Can you run the code in debug mode and see if the controller is being hit, and if it is what the values of IFormFile file and int IdResource is? – Sam Apr 23 '20 at 08:37
  • I´ll update now the question, and yes the controller is hitted, gonna show now – Qiqke Apr 23 '20 at 08:39
  • 2
    Oh, that makes sense then. But I think that might be what you're doing wrong actually. If you're sending your POST request to the url that you want the image to be saved at, that url naturally doesn't exist yet. So I'm guessing that you have to send the request to some other endpoint on the server and specify your desired filename in the request body, but I don't know the service that you're using, so unfortunately I don't know how their API is structured – sunero4 Apr 23 '20 at 08:46
  • Yes!!, so I should ask to the people on server side how to handle this situation? They should give more info about where to target ? @sunero4 – Qiqke Apr 23 '20 at 08:49
  • Yea if you are in a position where you can contact them I think that would be the way to go :) – sunero4 Apr 23 '20 at 08:53

1 Answers1

1

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

Sam
  • 394
  • 2
  • 4
  • 9