2

I'm trying to upload the picked file from the react native app using expo-image-picker, but at the backend side I can't see anything.

At the start I used this solution, but the IFormFile was null there.

[HttpPost]
    public IActionResult Upload([FromBody] ReqFile reqFile)
    {
        try
        {
            using (var ms = new MemoryStream())
            {
                reqFile.file.CopyTo(ms);
                var fileBytes = ms.ToArray();

                fileBytes = _fileService.AsJpeg(fileBytes);
                fileBytes = _fileService.Resize(fileBytes, 500);
                fileBytes = _fileService.Compress(fileBytes);

                var fileGuid = _fileService.Upload(fileBytes, reqFile.file.ContentType);
                return Ok(fileGuid.ToString());
            }
        }
        catch (Exception ex)
        {
            return BadRequest();
        }
    }

Where the DTO is

public class ReqFile {
    public IFormFile file {get; set;}
}

And image picker function is

export const getImageUUID = async (imagePickerResult) => {
// ImagePicker saves the taken photo to disk and returns a local URI to it
let localUri = imagePickerResult.uri;
let filename = localUri.split("/").pop();

// Infer the type of the image
let match = /\.(\w+)$/.exec(filename);
let type = match ? `image/${match[1]}` : `image`;

let formData = new FormData();

const d = {
  uri: localUri,           
  name: filename,
  type: type
}

formData.append("file", d);

return await axios.post(FILE_URI,formData,
  {
    headers: {
      'Content-Type': 'multipart/form-data',
      'Accept': 'application/json'
    }
  });
};

Now I'm trying to resolve it using the blob

export const getImageUUID = async (imagePickerResult) => {
// ImagePicker saves the taken photo to disk and returns a local URI to it
let localUri = imagePickerResult.uri;
let filename = localUri.split("/").pop();

// Infer the type of the image
let match = /\.(\w+)$/.exec(filename);
let type = match ? `image/${match[1]}` : `image`;

let formData = new FormData();

const rp = await fetch(localUri);
const blob = await rp.blob();

formData.append("file", blob);

return await axios.post(FILE_URI,formData,
  {
    headers: {
      'Content-Type': 'multipart/form-data',
      'Accept': 'application/json'
    }
  });
};

But I got the not found error during requesting the file screenshot

0 Answers0