react native android 上传文件,Nodejs服务端获取上传的文件
React Native端
使用react-native-image-picker 做出选择图片的操作,选择完成后,直接将图片Post至服务器,保存在服务器的某个地方(保存图片的路径需要公开显示),并返回图片的URL地址,Android应用中,只保存图片的URL地址。
component中代码
_chooseImage() {
ImagePicker.showImagePicker(options, (response) => {
console.log('Response = ', response);
if (response.didCancel) {
console.log('用户取消了选择!');
}
else if (response.error) {
alert("ImagePicker发生错误:" + response.error);
}
else if (response.customButton) {
alert("自定义按钮点击:" + response.customButton);
}
else {
// let source = { uri: response.uri };
try {
console.log(response);
uploadImage(response.uri, response.fileName, this.state.userEntity.token).then((imageUrl) => {
console.log(imageUrl);
//http://f11.baidu.com/it/u=3806443343,4236894804&fm=72
// You can also display the image using data:
let source = { uri: 'data:image/jpeg;base64,' + response.data };
let oldselectIssueData = this.state.selectIssueData;
oldselectIssueData.IssueImageList.push({ uri: imageUrl });
console.log('设置后')
this.setState({
selectIssueData: oldselectIssueData
});
}).catch((err) => {
throw err;
})
} catch (error) {
Alert.alert('选择图片错误', error.message);
}
}
});
}
公共方法 uploadFile.js
import { default_API_url } from '../config/index';
export function uploadImage(imageUri, imageName, token) {
return new Promise((resolve, reject) => {
let data = new FormData()
if (imageUri) {
data.append('image', { uri: imageUri, name: imageName, type: 'image/jpg' })//加入图片
}
const config = {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'multipart/form-data',
// 'Content-Language': React.NativeModules.RNI18n.locale,
'Authorization': token,
},
body: data,
}
fetch(default_API_url + '/fittingImage', config)
.then(function (response) {
return response.json();
}).then((responseData) => {
console.log('responseData', responseData);
if (responseData.resultType == 'SUCCESS') {
return resolve(default_API_url + '/' + responseData.results.imageName);//返回的是文件名,这里将它加入服务器的名称
} else {
return reject(responseData.resultMsg || 'Unknow Error');
}
}).catch((error) => {
return reject(error);
});
});
}
export default uploadImage;
Nodejs端,后端使用multer作为上传文件中间件,参考地址:http://cnodejs.org/topic/564f32631986c7df7e92b0db和https://dzone.com/articles/upload-files-or-images-to-server-using-nodejs
service.js中,增加
app.use(express.static(config.fittingImagePath));//公开某个文件夹
router中的方法:
exports.UploadFittingImage = (req, res) => {
try {
upload(req, res, function (err) {
if (err) {
return res.json({ url: err.message })
}
logger.info('req.files---------------------', req.files[0].filename)//因为支持的是多文件上传,这里我返回的是第一个文件名
return res.status(200).json(responseFormat(null, { imageName: req.files[0].filename }));
});
}
catch (error) {
return res.status(400).json(responseFormat(error, null));
}
};
公共方法fileUpload.js
import multer from 'multer';
import Config from 'config-lite';
let config = Config(__dirname); let Storage = multer.diskStorage({
destination: function (req, file, callback) {//文件保存位置,我这里是配置的
callback(null, "./" + config.fittingImagePath);
},
filename: function (req, file, callback) {//重命名文件
callback(null, file.fieldname + "_" + Date.now() + "_" + file.originalname);
}
}); let upload = multer({
storage: Storage
}).array("image", 1); //Field name and max count
//导出对象
module.exports = upload;
整体的流程

react native android 上传文件,Nodejs服务端获取上传的文件的更多相关文章
- springboot 服务端获取前端传过来的参数7种方式
下面为7种服务端获取前端传过来的参数的方法 1.直接把表单的参数写在Controller相应的方法的形参中,适用于GET 和 POST请求方式 这种方式不会校验请求里是否带参数,即下面的userna ...
- React Native Android启动白屏的一种解决方案上
我们用RN去开发Android应用的时候,我们会发现一个很明显的问题,这个问题就是启动时每次都会有1~3秒的白屏时间,直到项目加载出来 为什么会出现这个问题? RN开发的应用在启动时,首先会将js b ...
- Node.js:上传文件,服务端如何获取文件上传进度
内容概述 multer是常用的Express文件上传中间件.服务端如何获取文件上传的进度,是使用的过程中,很常见的一个问题.在SF上也有同学问了类似问题<nodejs multer有没有查看文件 ...
- Wince 6.0适用 .NET 使用HttpRequest的Post上传文件,服务端的Web API接收Post上传上来的文件 代码
//调用的示例 private string fileName = "InStorageData.csv"; string filePath = parentPath + Comm ...
- react-native —— 在Mac上配置React Native Android开发环境排坑总结
配置React Native Android开发环境总结 1.卸载Android Studio,在终端(terminal)执行以下命令: rm -Rf /Applications/Android\ S ...
- MVC文件上传04-使用客户端jQuery-File-Upload插件和服务端Backload组件实现多文件异步上传
本篇使用客户端jQuery-File-Upload插件和服务端Badkload组件实现多文件异步上传.MVC文件上传相关兄弟篇: MVC文件上传01-使用jquery异步上传并客户端验证类型和大小 ...
- MIME类型-服务端验证上传文件的类型
MIME的作用 : 使客户端软件,区分不同种类的数据,例如web浏览器就是通过MIME类型来判断文件是GIF图片,还是可打印的PostScript文件. web服务器使用MIME来说明发送数据的种类, ...
- 【2017-05-30】WebForm文件上传。从服务端删除文件
用 FileUpload控件进行上传文件. <asp:FileUpload ID="FileUpload1" runat="server" /> ...
- asp.net上传Excel文件到服务端进行读取
1.我们IIS是使用7.5,由于在网站中上传Excel文件到服务端进行数据读取时候出现读取失败情况.一开始以为是没有按照office软件问题,其实不然,因为server是64位操作系统,如果我们要使用 ...
随机推荐
- Qt ---------- connect连接类型
Qt::AutoConnection 0 (Default) If the receiver lives in the thread that emits the signal, Qt::Direct ...
- 应用Hash函数
本文系转载,转自:http://www.blogjava.net/jinfei0627/articles/219543.html 应用Hash函数(java描述) 作者:冲处宇宙 时间:2007.1. ...
- 【51NOD-0】1081 子段求和
[算法]树状数组(区间和) [题解]记得开long long #include<cstdio> #include<cstring> #include<algorithm& ...
- php trait 变量类型为数组时 不能被父类子类同时use
直接上代码 --------------------------- trait T1 { public static $a=1; public static $b= []; public static ...
- 教你 Shiro 整合 SpringBoot,避开各种坑(山东数漫江湖)
依赖包 <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-sprin ...
- Problem E. Matrix from Arrays(杭电2018年多校第四场+思维+打表找循环节)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6336 题目: 题意:给你一个l个元素的数组a,用题目中的程序构造一个新的矩阵,询问q次,问以(x1,y ...
- apache log 按日期记录 格式 <GOOD>-- (转)
在apache的配置文件中找到ErrorLog logs/error_logCustomLog logs/access_log common Linux系统配置方法: 将其改为ErrorLog “| ...
- 空间数据库系列一:geomesa&sparksql 分析环境搭建
geomesa sparksql 分析环境搭建 1.安装hbase-1.3.2.1 standlone版本,作为geomesa的store a.修改配置文件:hbase-1.3.2.1/conf/hb ...
- O(1)时间复杂度实现入栈、出栈、获得栈中最小元素、获得栈中最大元素(转)
题目要求:定义栈的数据结构,添加min().max()函数(动态获取当前状态栈中的最小元素.最大元素),要求push().pop().min().max()的时间复杂度都是O(1). 思路解析:根据栈 ...
- 《Linux内核原理与设计》第十一周作业 ShellShock攻击实验
<Linux内核原理与设计>第十一周作业 ShellShock攻击实验 分组: 和20179215袁琳完成实验及博客攥写 实验内容: Bash中发现了一个严重漏洞shellshock, ...