1

I want to upload an image file from react native to my server

React native code:

uploadData = () => {
let formdata = new FormData();
formdata.append('PhotoFile', {
    uri: this.state.image,
    type: 'image/png',
    name: 'photo'
  });
formdata.append('Title', this.state.title);

fetch('https://link/api/memes',{
  method: 'POST',
  headers: {
    'Content-Type': 'multipart/form-data',
  },
  body: formdata
  }).then(response => {
    alert(response.status);
  }).catch(err => {
    alert(err);
  });  
}

I am using expo-image-picker to pick an image, and i store it in state

My Controller has a following prototype:

[HttpPost]
public async Task<IActionResult> CreateTitle([FromForm] TitleCreateDto titleCreateDto)

TitleCreateDto class lookes like this:

 public class TitleCreateDto
{
    public TitleCreateDto()
    {
        DateAdded = DateTime.Now;
    }

    public string Title { get; set; }
    public DateTime DateAdded { get; set; }
    public IFormFile PhotoFile { get; set; }
}
2
  • Can you check out this answer Commented Aug 29, 2020 at 16:56
  • this.state.image what's data you assigned to it? Is it Base64-encoded string or local path to image? Besides, you define PhotoFile property as IFormFile in your model class but you provide PhotoFile with json object in formdata. Commented Sep 1, 2020 at 14:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.