[英]File Upload Angular 2 & Sails Js
I am trying to upload multiple files from Angular 2 and Sails Js server. 我试图从Angular 2和Sails Js服务器上传多个文件。 I want to place file inside public folder of SailJs App.
我想将文件放在SailJs App的公共文件夹中。
The file is uploaded from Angular 2 App by getting file from an event fired. 通过从触发的事件中获取文件,从Angular 2 App上载该文件。 the code for single file upload is as follows:
单个文件上传的代码如下:
Angular 2 service: Angular 2服务:
fileChange(event: any): Promise<string> {
let fileList: FileList = event.target.files;
if(fileList.length > 0) {
let file: File = fileList[0];
let formData:FormData = new FormData();
formData.append('myFile', file, file.name);
let headers = new Headers();
let cToken = this.cookie.getCookie("token");
headers.append('Authorization', 'Bearer ' + cToken);
headers.append('Content-Type', undefined);
//headers.append('Content-Type', 'multipart/form-data');
headers.append('Accept', 'application/json');
let options: RequestOptionsArgs = { headers: headers, withCredentials: true }
return new Promise((resolve, reject) => {
this.http.post( this.apiEndpoint + "project/reffile/add/all", formData, options).toPromise()
.then(response => {
// The promise is resolved once the HTTP call is successful.
let jsonData = response.json();
if (jsonData.apiStatus == 1) {
resolve(jsonData);
}
else reject(jsonData.message);
})
// The promise is rejected if there is an error with the HTTP call.
// if we don't get any answers the proxy/api will probably be down
.catch(reason => reject(reason.statusText));
});
}
}
SailsJs method: SailsJs方法:
/**
* `FileController.upload()`
*
* Upload file(s) to the server's disk.
*/
addAll: function (req, res) {
// e.g.
// 0 => infinite
// 240000 => 4 minutes (240,000 miliseconds)
// etc.
//
// Node defaults to 2 minutes.
res.setTimeout(0);
console.log("req.param('filename')");
console.log(req.param('filename'));
req.file('myFile')
.upload({
// You can apply a file upload limit (in bytes)
maxBytes: 1000000
}, function whenDone(err, uploadedFiles) {
if (err) return res.serverError(err);
else return res.json({
files: uploadedFiles,
textParams: req.allParams()
});
});
},
after posting form, I didn't get file in HTTP
call response also not able to console.log(req.param('filename'));
发布表单后,我没有得到
HTTP
调用响应中的文件也无法访问console.log(req.param('filename'));
. 。
please help me what I am doing wrong here. 请帮助我,我在这里做错了什么。 I also tried changing/removing header, but still not working,
我也试过更改/删除标题,但仍然无法正常工作,
some expert says that HTTP
currently cant upload files, need to use native XHR request for this. 有些专家说
HTTP
目前无法上传文件,需要使用本机XHR请求。 please view Thierry Templier 's answer here 请在这里查看Thierry Templier的答案
Try specifying a directory for file upload: 尝试指定文件上传目录:
req.file('file').upload({
dirname: '../../assets/uploads'
},function (err, files) {
if (err) return res.serverError(err);
var fileNameArray = files[0].fd.split("/");
var fileName = fileNameArray[fileNameArray.length - 1];
console.log("fileName: ",fileName);
});
To access the uploaded file - you can append the fileName to the upload directory that you have specified. 要访问上载的文件,您可以将fileName附加到您指定的上载目录。 File will be accessible
文件将可访问
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.