I want to send file from node.js server to client on Angular. I'm using fileSaver.js on client.
Server:
res.download('123.txt', function(err){
if(err){
console.log('error');
}
})
Cliet:
$http.post('/downloadFile').success(function(res){
var file = new Blob([res]);
saveAs(file, '123.txt');
}
But it works only with .txt files.
How to do this with other formats?
check this:
Failed to load PDF document - Angular JS - BLOB
set responseType:
$http.post('/downloadFile','', {responseType:'arraybuffer'}).success(function(res){.....}
Related
I'm using AngularJS 1.6 to send a file to the server. The server uses Multer to read the files and I am providing a secure access token on each file uploaded.
The project uses the ng-file-upload which uses the Upload.upload() to send files. The issue pops when you have an access token to send in conjunction with the file. I don't know what is happening, But there are no parameters sent to the server using Upload.upload(). I tried using $http and this method sends the parameters but does not upload the file to server at request.file.
The front end code looks like this
Upload.upload({
url: '/upload_to_file_path',
arrayKey: '',
headers: {'Content-Type': undefined},
method: 'POST',
file: file,
data:
{
email: someData.email,
auth_token: someData.auth_token,
},
})
When I use $http for the above it sends the token but not the file.
On the back end,
exports.uploads = (req, res, err) =>
{
if (req.file == null)
{
//File is unable to be read here
res.send({ msg: "filename is undefined", success: true, statusCode: 422 });
return;
}
I'm losing my head over this as I'm relatively new to AngularJS. I've used Multer before so I doubt the problem is occurring on the server.
I would appreciate some help. I have been looking up for the solution for some time now, But I'm afraid I'm not able to find out what's wrong.
Thanks for the help
In my serverless app, I need to generate pdf dynamically and then upload that generated pdf into aws-s3 bucket. But in serverless, we can only sent json request, so I sent my html string to serverless, it generate pdf and then save that generated pdf into local machine. I think, that part I can do, But my problem is, I need to upload that newly generated pdf into aws-s3. My code is given below:
Angular Code:
$scope.generate_pdf = function() {
var html = angular.element('html').html();
var service = API.getService(); // sent html string as json for generating pdf
service.downloadPdf({}, { html : html },
function(res) {
console.log("res : ", res); // res.url: /tmp/dynamica-generate.pdf
// pdf is saved into '/tmp' folder
// now I want to upload pdf from '/tmp' folder to aws-s3
}, function(err) {
console.log("err : ", err);
});
};
How can I solve this problem, Thanks in Advance.
If you want to upload file from your local machine to S3 bucket. Install AWS Cli on your local machine. after that is done add a cron job for this command
aws s3 sync /tmp/* s3://mybucket
You should be able to use the aws Javascript S3 sdk to integrate JS code into your angularJS code.
You can follow from this blog post and github code
Its also mentioning about setting up the credentials using a public IAM account. Depending how you serve the files to your clients, you might check also using Pre-Signed URL’s.
Im a mobile app developer and new to web apps. Im trying to implement upload and download a file in the web app using MeanJS stack. I have used multer plugin to upload file to server. After a lot of googling I have successfully used Multer which uploads and saves the files to uploads directory.
Now Im trying to implement download the same, and I don't have an idea to do this. I have followed two methods:
<a target="_self" href="/uploads/{{eachDocument.modifiedFileName}}" download="{{eachDocument.fileName}}">{{eachDocument.fileName}}</a>
Have used a route in the express server controller, as given in http://expressjs.com/api.html#res.download
Using the method #1 application never downloads the file with an error "Not a file", because uploads is not the public directory.
Using method #2, I'm able to get the raw file content, but not sure how to save the file.
Can anyone please tell me how to implement the file download that are saved in uploads directory that were uploaded using multer.
Thanks
#Sridhar Chidurala, please find my code for the server and view controller:
I'm using the following code for server controller :
var file = __dirname + "/../../" + results.filePath;
res.download(file, results.filename, function (err) {
if (err) {
} else {
}
});
On the view controller, Im posting some data and based on the data I get the file data to the view.:
$http.post(requestString, reqObject).
success(function (response) {
// **I get the file data here**
console.log("success response : " + JSON.stringify(response));
}).error(function (response) {
console.log("error response : " + JSON.stringify(response));
});
I'm using the ng-Cordova fileTransfer plugin to try to upload a photo from the user's camera roll to a Node/Express server.
I'm getting the local URI of a photo and trying to pass it to the plugin as such:
$cordovaFileTransfer.upload('http://135.23.98.169:8069/upload_avatar', fileURI)
.then(function (result) {
console.log('success');
}, function (error) {
console.log('what');
});
The server endpoint is reached, but req.files is undefined.
What am I doing wrong?
In Express 4.x, you have to include the multer middleware to enable multipart data. After you've done that, the file will available in req.files
I have in my backend application an spray route that pushes out a .xls file using
FileUtils.readAllBytes(xlsFile)
In my frontend application I'm using angularjs with Restangular module so I need to receive my .xls file and download it to user's computer.
I have in my code
Restangular.one("localhost:9090/excel")
.get()
.then(function(response){
alert("Excel is here!");
var hiddenElement = document.createElement('a');
hiddenElement.href = 'data:attachment/xls,' + encodeURIComponent(response);
hiddenElement.download = 'reporte-anual.xls';
hiddenElement.click();
});
but it only does the alert.
How can I achieve that?
Thank you all