React Native (Expo) загрузить файл - PullRequest
0 голосов
/ 16 апреля 2020

Я пытаюсь загрузить выбранный файл из собственного реагирующего приложения, используя expo-image-picker, но на бэкэнде я ничего не вижу.

В начале я использовал это решение , но IFormFile там был нулевым.

[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();
        }
    }

Где DTO -

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

, а функция выбора изображения -

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'
    }
  });
};

Сейчас Я пытаюсь разрешить его с помощью 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'
    }
  });
};

Но я получил не найденную ошибку при запросе файла снимок экрана

...