美文网首页
React-Native fetch上传图片

React-Native fetch上传图片

作者: Andy_Sun | 来源:发表于2017-10-25 16:49 被阅读0次

项目有上传图片需求,查找一番发现第三方的很麻烦,还要适配手机系统.在此记录一下如何使用fetch上传.


uploadImage(imgUrl) {

let url = Url.ossFile; //图片服务器

let formData =new FormData();// 把图片放入formData中,采用formData来实现

let file = { uri:  imgUrl, type: 'multipart/form-data', name: 'image.png' };// 这里的key(uri和type和name)不能改变,此处type也有可能是'application/octet-stream',看后台配置

formData.append('file', file); // 有可能是file 也有可能是images 看后台的配置

return fetch(url, {

method: 'POST',

headers:  {

'Content-Type': 'multipart/form-data; charset = utf-8'

},

body: formData

})

.then((response) => response.text())

.then((responseData) => {

console.log('responseData', responseData);

})

.catch((error) => {console.error('error', error) });

}

这样即可上传完成.

相关文章

网友评论

      本文标题:React-Native fetch上传图片

      本文链接:https://www.haomeiwen.com/subject/vfzqpxtx.html