简体   繁体   English

使用 HttpClient 将图像上传到 URI

[英]Upload Image to URI using HttpClient

I´ma newbie about http related things, so I am a little stacked.我是关于 http 相关事情的新手,所以我有点堆积。

What I want is simple, I need to POST, upload a imaged to a server like this: http://web.com/imageGalerry .我想要的很简单,我需要发布,将图像上传到这样的服务器: 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:以及我用来处理视图的 dropzone js:

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这是我从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过去,在将图像作为 IFormFile 上传到 ASP.Net controller 时,我发现以下两段代码很重要

On the form tag in your view add the enctype attribute在视图中的表单标签上添加 enctype 属性

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

For an explanation of what multipart/form-data means: What does enctype='multipart/form-data' mean?有关 multipart/form-data 含义的解释: enctype='multipart/form-data' 是什么意思?

Then add the right binding to your controller parameters然后将正确的绑定添加到您的 controller 参数

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我不确定这将如何与 dropzone 一起使用,但如果 IFormFile 在 controller 中显示为 null 这可能值得一试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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