Node.js 将照片从手机上传到节点服务器的最佳方式是什么
我想知道什么是将照片从手机(使用react native)上传到节点服务器的最佳方式 目前我用base64编码我的图片,并将其存储在一个长文本中 但是有没有更有效的方法呢 我正在使用Node.js 将照片从手机上传到节点服务器的最佳方式是什么,node.js,rest,react-native,sequelize.js,Node.js,Rest,React Native,Sequelize.js,我想知道什么是将照片从手机(使用react native)上传到节点服务器的最佳方式 目前我用base64编码我的图片,并将其存储在一个长文本中 但是有没有更有效的方法呢 我正在使用 'Content-Type': 'application/x-www-form-urlencoded' 到达我的API Thank是一个node.js中间件,用于处理多部分/表单数据,主要用于上传文件 复制粘贴文档中的代码非常简单 我建议使用formdata而不是base64 P>速度和效率,可能考虑在传输
'Content-Type': 'application/x-www-form-urlencoded'
到达我的API
Thank是一个node.js中间件,用于处理多部分/表单数据,主要用于上传文件
复制粘贴文档中的代码非常简单 我建议使用formdata而不是base64 <> P>速度和效率,可能考虑在传输和PrHARPs之前先调整图像大小,为客户端的浏览创建缩略图。 此示例Im使用Axios、“反应本机图像选择器”、“反应本机图像大小调整器”和Redux Api.js
export const api = axios.create({
baseURL: server,
headers: {
'Cache-Control': 'no-cache'
},
timeout: 5000
})
uploadPicture = (photo) => {
api.post('/image/'+this.state.position, photo)
.then(() => {
this.props.getThumbList()
.then((response) => {
this.props.setThumbSource(response.payload.data)
this.setState({thumbUri: {uri: this.props.thumbSource[this.state.position]}})
})
.catch((error) => {
console.log(this.props.errorText)
})
})
.catch((error) => {
console.log(this.props.errorText)
})
}
openImagePicker = () => {
// get image from image picker
ImagePicker.showImagePicker(this.options, async response => {
this.setState({
originUri: response.uri
})
if (response.didCancel) {
console.log('User cancelled image picker')
return
} else if (response.error) {
console.log('ImagePicker Error: ', response.error)
return
} else if (response.customButton) {
console.log('User tapped custom button: ', response.customButton)
return
}
//Post the photo with position which it was selected
const photo = new FormData();
// data.append('authToken', 'secret');
photo.append('photo', {
uri: this.state.originUri,
type: 'image/jpeg',
name: 'p-'+this.state.position+'.jpg'
});
let { height, width, quality, format, originUri } = this.state
// Resize and post the thumb
const resizedImageUri = await ImageResizer.createResizedImage(
originUri,
height,
width,
format,
quality
).then(({uri}) => {
photo.append('thumb', {
uri: uri,
type: 'image/jpeg',
name: 't-'+this.state.position+'.jpg'
});
this.uploadPicture(photo);
})
})
}
export const GET_THUMB_LIST = 'GET_THUMB_LIST';
export const GET_THUMB_LIST_SUCCESS = 'GET_THUMB_LIST_SUCCESS';
export const GET_THUMB_LIST_FAIL = 'GET_THUMB_LIST_FAIL';
export const SET_THUMB_SOURCE = 'SET_THUMB_SOURCE';
export const SET_THUMB_SOURCE_FAIL = 'SET_THUMB_SOURCE_FAIL';
export function getThumbList() {
return {
type: GET_THUMB_LIST,
payload: {
request: {
method: 'GET',
url:'/thumbs'
}
}
};
}
export function setThumbSource(list) {
return {
type: SET_THUMB_SOURCE,
payload: list
};
}
export default function reducer(state = {}, action) {
switch (action.type) {
case GET_THUMB_LIST_SUCCESS:
// console.log(action.payload.data)
return {
...state,
thumbList: action.payload.data
}
case GET_THUMB_LIST_FAIL:
return {
...state,
errorText: "Cannot get image list"
}
case SET_THUMB_SOURCE:
return {
...state,
thumbSource: action.payload
}
case SET_THUMB_SOURCE_FAIL:
return {
...state,
errorText: "Set thumb uri failed"
}
default:
return state
}
}
PhotoUpload.js
export const api = axios.create({
baseURL: server,
headers: {
'Cache-Control': 'no-cache'
},
timeout: 5000
})
uploadPicture = (photo) => {
api.post('/image/'+this.state.position, photo)
.then(() => {
this.props.getThumbList()
.then((response) => {
this.props.setThumbSource(response.payload.data)
this.setState({thumbUri: {uri: this.props.thumbSource[this.state.position]}})
})
.catch((error) => {
console.log(this.props.errorText)
})
})
.catch((error) => {
console.log(this.props.errorText)
})
}
openImagePicker = () => {
// get image from image picker
ImagePicker.showImagePicker(this.options, async response => {
this.setState({
originUri: response.uri
})
if (response.didCancel) {
console.log('User cancelled image picker')
return
} else if (response.error) {
console.log('ImagePicker Error: ', response.error)
return
} else if (response.customButton) {
console.log('User tapped custom button: ', response.customButton)
return
}
//Post the photo with position which it was selected
const photo = new FormData();
// data.append('authToken', 'secret');
photo.append('photo', {
uri: this.state.originUri,
type: 'image/jpeg',
name: 'p-'+this.state.position+'.jpg'
});
let { height, width, quality, format, originUri } = this.state
// Resize and post the thumb
const resizedImageUri = await ImageResizer.createResizedImage(
originUri,
height,
width,
format,
quality
).then(({uri}) => {
photo.append('thumb', {
uri: uri,
type: 'image/jpeg',
name: 't-'+this.state.position+'.jpg'
});
this.uploadPicture(photo);
})
})
}
export const GET_THUMB_LIST = 'GET_THUMB_LIST';
export const GET_THUMB_LIST_SUCCESS = 'GET_THUMB_LIST_SUCCESS';
export const GET_THUMB_LIST_FAIL = 'GET_THUMB_LIST_FAIL';
export const SET_THUMB_SOURCE = 'SET_THUMB_SOURCE';
export const SET_THUMB_SOURCE_FAIL = 'SET_THUMB_SOURCE_FAIL';
export function getThumbList() {
return {
type: GET_THUMB_LIST,
payload: {
request: {
method: 'GET',
url:'/thumbs'
}
}
};
}
export function setThumbSource(list) {
return {
type: SET_THUMB_SOURCE,
payload: list
};
}
export default function reducer(state = {}, action) {
switch (action.type) {
case GET_THUMB_LIST_SUCCESS:
// console.log(action.payload.data)
return {
...state,
thumbList: action.payload.data
}
case GET_THUMB_LIST_FAIL:
return {
...state,
errorText: "Cannot get image list"
}
case SET_THUMB_SOURCE:
return {
...state,
thumbSource: action.payload
}
case SET_THUMB_SOURCE_FAIL:
return {
...state,
errorText: "Set thumb uri failed"
}
default:
return state
}
}
Redux.js
export const api = axios.create({
baseURL: server,
headers: {
'Cache-Control': 'no-cache'
},
timeout: 5000
})
uploadPicture = (photo) => {
api.post('/image/'+this.state.position, photo)
.then(() => {
this.props.getThumbList()
.then((response) => {
this.props.setThumbSource(response.payload.data)
this.setState({thumbUri: {uri: this.props.thumbSource[this.state.position]}})
})
.catch((error) => {
console.log(this.props.errorText)
})
})
.catch((error) => {
console.log(this.props.errorText)
})
}
openImagePicker = () => {
// get image from image picker
ImagePicker.showImagePicker(this.options, async response => {
this.setState({
originUri: response.uri
})
if (response.didCancel) {
console.log('User cancelled image picker')
return
} else if (response.error) {
console.log('ImagePicker Error: ', response.error)
return
} else if (response.customButton) {
console.log('User tapped custom button: ', response.customButton)
return
}
//Post the photo with position which it was selected
const photo = new FormData();
// data.append('authToken', 'secret');
photo.append('photo', {
uri: this.state.originUri,
type: 'image/jpeg',
name: 'p-'+this.state.position+'.jpg'
});
let { height, width, quality, format, originUri } = this.state
// Resize and post the thumb
const resizedImageUri = await ImageResizer.createResizedImage(
originUri,
height,
width,
format,
quality
).then(({uri}) => {
photo.append('thumb', {
uri: uri,
type: 'image/jpeg',
name: 't-'+this.state.position+'.jpg'
});
this.uploadPicture(photo);
})
})
}
export const GET_THUMB_LIST = 'GET_THUMB_LIST';
export const GET_THUMB_LIST_SUCCESS = 'GET_THUMB_LIST_SUCCESS';
export const GET_THUMB_LIST_FAIL = 'GET_THUMB_LIST_FAIL';
export const SET_THUMB_SOURCE = 'SET_THUMB_SOURCE';
export const SET_THUMB_SOURCE_FAIL = 'SET_THUMB_SOURCE_FAIL';
export function getThumbList() {
return {
type: GET_THUMB_LIST,
payload: {
request: {
method: 'GET',
url:'/thumbs'
}
}
};
}
export function setThumbSource(list) {
return {
type: SET_THUMB_SOURCE,
payload: list
};
}
export default function reducer(state = {}, action) {
switch (action.type) {
case GET_THUMB_LIST_SUCCESS:
// console.log(action.payload.data)
return {
...state,
thumbList: action.payload.data
}
case GET_THUMB_LIST_FAIL:
return {
...state,
errorText: "Cannot get image list"
}
case SET_THUMB_SOURCE:
return {
...state,
thumbSource: action.payload
}
case SET_THUMB_SOURCE_FAIL:
return {
...state,
errorText: "Set thumb uri failed"
}
default:
return state
}
}
请避免问“做X的最佳方式”之类的问题。而是针对你想要做什么的实际需求。