react-native - React Native (Expo) 上传文件
问题描述
我正在尝试使用 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'
}
});
};
但是我在请求文件 截图时遇到了未找到的错误
解决方案
推荐阅读
- python - 如何获得长度为 n 的 1 和 2 的所有可能组合,其中 2s 不在一起?
- nestjs - TypeORM 中我的多对多关系中的一个错误
- javascript - 在 Firestore 限制中使用变量
- javascript - Sequelize 使用 AND 和 OR 子句构建动态 where 子句条件
- javascript - 返回的变量没有从 API 调用中获得正确的值
- java - 无法通过 Redmi9 和联想平板电脑的蓝牙低功耗连接
- java - 如何在 android studio 中启用/禁用 Aeroplan 模式或启用/禁用移动数据
- javascript - React Native:_chalk.constructor 不是构造函数
- amazon-web-services - 共享我的 lightsail 实例公共静态 IP 是否安全
- python - 在链表的索引处插入