Hao*_*hen 6 express multer react-native
React Native IOS应用程序,想上传图片; 从设备.RN 0.39.2
客户:
const formData = new FormData()
formData.append('files', file)
formData.append('type', 'image')
fetch(API_HOST+UPLOAD_AVATAR,{
method:'post',
headers: {'Content-Type':'multipart/form-data;boundary=6ff46e0b6b5148d984f148b6542e5a5d','Authorization': 'Bearer'+' '+token},
body: formData
})
.then(response=>response.json())
.then(data=>{
//console.log(data)
//Alert.alert(data)
})
.catch(error=>{
console.log(error)
})
Run Code Online (Sandbox Code Playgroud)
服务器:
var multer = require('multer');
var upload = multer();
router.post('/user', ensureAuthenticated, upload.any(), function (req, res) {
console.log(req.body);
console.log(req.files);
})
Run Code Online (Sandbox Code Playgroud)
错误:
server req.body和req.files为空.
然后我尝试使用RNFetchBlob.
RNFetchBlob.fetch('POST', API_HOST+UPLOAD_AVATAR, {
'Content-Type':'multipart/form-data;boundary=6ff46e0b6b5148d984f148b6542e5a5d'
'Authorization' : 'Bearer'+' '+token
}, formData)
.then((resp) => {
}).catch((err) => {
// ...
})
Run Code Online (Sandbox Code Playgroud)
然后错误改为
NSMutableDictionary无法转换为NSString.
而且req.body是{},req.files是未定义的
小智 0
我想您已经找到了解决方案,如果是的话,您能分享一下吗?\n无论如何,对于 RNFetchBlob 问题,我曾经遇到过相同的错误,我通过将 FormData 更改为数组来解决。像这样:
\n\nconst body = [{\n name: 'data',\n data: JSON.stringify(whateverData)\n}, {\n name: 'file',\n data: filePath,\n}]; \n\n\xe2\x80\xa6\n\nRNFetchBlob.fetch('POST', apiEndpoint, headers, body);\nRun Code Online (Sandbox Code Playgroud)\n\n希望有帮助。
\n| 归档时间: |
|
| 查看次数: |
260 次 |
| 最近记录: |