How to upload image from React to ASP.NET Core Web API?

时光怂恿深爱的人放手 提交于 2021-01-27 16:02:12

问题


I have Web API controller in mt server side

[HttpPost("CreateImage")]
    public void CreateImage([FromBody] ImageDTO img)
    {
        Image image = new Image { FileName = img.FileName };
        byte[] imageData = null;
        using (var binaryReader = new BinaryReader(img.Image.OpenReadStream()))
        {
            imageData = binaryReader.ReadBytes((int)img.Image.Length);
        }
        image.Picture = imageData;

        imageRepo.Create(image);

    }

Where ImageDTO is

     public class ImageDTO
    {
        public string FileName { get; set; }

        public IFormFile Image { get; set; }
    }

and Image.cs like this

public class Image
   {
      public int Id { get; set; }

      public string FileName{ get; set; }

      public byte[] Picture { get; set; }

      public List<User> Users { get; set; }
   }

And this is what I using for handling and sending image on React client:

<form>
        <p>
            <label>Аватар</label>
            <input name="Avatar" id = 'img' type="file" class="form-control" onChange={(e)=>this.handleImageChange(e)}/>
        </p>
        <p>
            <input type="submit" value="Добавить" onClick={this.sendImage}/>
        </p>
</form>
      <div className="imgPreview">
      {$imagePreview}
    </div>

function for handling file into state

    handleImageChange(e) {
    e.preventDefault();
    let form = new FormData();
    for (var index = 0; index < e.target.files; index++) {
      var element = e.target.files[index];
      form.append('file', element);
  }
    this.setState({file: form});
  }

sending it on server

async sendImage(event) {
event.preventDefault();

console.log(this.state.file);
await addImage(this.state.file);
console.log('it works');}

addImge function:

addImage = async ( image)  => {

    await fetch('https://localhost:44331/api/users/CreateImage',
        {
            method: 'POST',
            mode: 'cors',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
                'Authorization': 'Bearer ' + sessionStorage.tokenKey
            },
             body:  JSON.stringify({
                FileName: 'Img',
                Image: image
            })
        }
    )
}

But when it`s sending request on server it return Error 400, which means "Bad Request". So I think it is may be wrong type of data sent or something like that. Maybe someone sees a mistake or something that can be fixed. Or someone could show a working example of sending images from React to Web Api server. I need your help guys!


回答1:


For uploading, you need to pay attention to points below:

  1. You need to use formdata with FromForm
  2. The fields in formdata should be corresponding to the model fields.

Steps:

  1. Change the Controller action.

    public void CreateImage([FromForm] ImageDTO img)
    {
    
    }
    
  2. Client Code:

    async sendImage(event) {
        event.preventDefault();
        console.log(this.state.file);
        await this.addImage(this.state.file);
        console.log('it works');
    };
    addImage = async (image) => {
        await fetch('https://localhost:44385/api/users/CreateImage',
            {
                method: 'POST',
                mode: 'cors',
                headers: {
                    'Accept': 'application/json',
                    'Authorization': 'Bearer ' + sessionStorage.tokenKey
                },
                body: this.state.file
            }
        )
    }
    
    handleImageChange(e) {
        e.preventDefault();
        let form = new FormData();
        for (var index = 0; index < e.target.files.length; index++) {
            var element = e.target.files[index];
            form.append('image', element);
        }
        form.append('fileName', "Img");
        this.setState({ file: form });
    };    
    


来源:https://stackoverflow.com/questions/55205135/how-to-upload-image-from-react-to-asp-net-core-web-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!