我有一个功能上传文件:
handleUpload: function (e) {
e.preventDefault();
var files = this.refs.fileupload.getDOMNode().files; //get all the files to be upload.
if (files.hasOwnProperty(0) //get the first file.
&& files[0] instanceof File) {
var formData = new FormData();
formData.append('fileUpload',
files[0]); //append file to be sent to the server.
formData['fileUpload'] = files[0]
Service.uploadContact(null,null,formData)
.then(function(data){
if (data.statusCode == 0) { //upload success
this.setState({
validationmessage: data.message,
success: true,
showSpinner: false
})
React.findDOMNode(this.refs.fileupload).value = '' //clear upload file.
this.filterContact_Paging(1); //refresh the contact list.
} else {
this.setState({
validationmessage: data.error, //upload fail error
success: false,
showSpinner: false
})
React.findDOMNode(this.refs.fileupload).value = '' //clear upload file.
}
}, function(_err){
console.log('err: ', _err)
})
}
},
代码进程上传使用请求模块:
serviceInterface[item.name] = function (params, csrfToken, formData) {
return new Promise(function (resolve, reject) {
var token = storage.get('token') || '',
request_params = {
url: CONFIG.apiPath + item.path,
method: item.method,
crossOrigin: true,
withCredentials: false,
json: true,
headers: {'Authorization': 'Bearer ' + token},
};
//This is where you append form data and in most cases for file upload.
if (formData) {
request_params['formData'] = formData;
}
//This is a cross site reference token
if (csrfToken) {
headers['x-csrf-token'] = csrfToken.token;
headers['x-csrf-sec'] = csrfToken.secret;
}
//This is where you determine post/get to send the data through body or querystring.
if (item.method == 'GET') {
request_params['qs'] = params;
} else {
request_params['body'] = params;
}
request(request_params, function (error, response, body) {
if (error) {
return reject(error);
}
if (response.statusCode === 418) {
return reject(body);
}
return resolve(body);
});
});
};
但是当我运行代码时,它会抛出一个错误: TypeError:self._form.on不是函数