Hi, I am testing nativescript-background-http to test upload a picture file to server.
I test using physical mobile device. Below is the coding file
==================================start of a.component.ts================================
==================================end of package.json================================
I am using the node.js restify to accept the file. I have tested using Postman and it works file. The code of the restify is as below
==================================start of restify index.js================================
var restify = require('restify');
var fs = require('fs');
var os = require('os');
var server = restify.createServer({
name: 'software-manufacture-backend-file',
version: '0.1.0'
});
var Throttle = require("stream-throttle").Throttle;
var outDir = "/mnt/diskc/p/restify-file/uploads/";
server.use(restify.acceptParser(server.acceptable));
server.use(restify.CORS());
server.use(restify.queryParser());
server.use(restify.gzipResponse());
server.use(function (req, res, next) {
console.log('it reach the restify-file api without body parser');
return next();
});
server.get('/test', function (req, res, next) {
res.send(200, 'SUCCESS');
res.end;
return next();
});
server.post('/file', function (req, res, next) {
console.log(req.params);
var fileName = req.headers["file-name"];
console.log("req.headers are", req.headers);
var logger = console;
logger.log(req.method + "Request! Content-Length: " + req.headers["content-length"] + ", file-name: " + fileName);
logger.dir(req.headers);
var out = outDir + "upload-" + new Date().getTime() + "-" + fileName;
logger.log("Output in: " + out);
var total = req.headers["content-length"];
var current = 0;
var shouldFail = req.headers["should-fail"];
req.pipe(new Throttle({ rate: 1024 * 2048 })).pipe(fs.createWriteStream(out, { flags: 'w', encoding: null, fd: null, mode: 0666 }));
req.on('data', function (chunk) {
current += chunk.length;
if (shouldFail && (current / total > 0.25)) {
logger.log("Error ");
var body = "Denied!";
res.send(408, "Die!", { "Content-Type": "text/plain", "Content-Length": body.length, "Connection": "close" });
res.end();
shouldFail = false;
logger.log("Terminated with error: [" + out + "]: " + current + " / " + total + " " +
Math.floor(100 * current / total) + "%");
}
else {
logger.log("Data [" + out + "]: " + current + " / " + total + " " + Math.floor(100 * current / total) + "%");
}
});
req.on('end', function () {
logger.log("Done (" + out + ")");
var body = "Upload complete!";
res.send(200, "Done!", { "Content-Type": "text/plain", "Content-Length": body.length });
res.end();
});
req.on('error', function (e) {
logger.log('it reach the file received error!');
logger.log(e);
});
});
process.on('uncaughtException', function (err) {
console.log('uncaught Exception is ', err);
});
server.listen(9191, function () {
console.log('%s listening at %s', server.name, server.url);
});
==================================end of restify index================================
==================================start of restify packson.json================================
When I use the Postman, I provide the following attirbutes
Method: Post
URL: http://it-enable.net:9191/file
Header: {"Content-Type":"application/octet-stream"}
Attached a picture file.
If the file is uploaded successfully, I can see the response of "Done!".
When I run the project in physical mobile device, I hit the error "error during upload." When I look at backend restify, I find that the nativescript-background-http hits error before it sends the file to backend restify. This is because the backend restify doesn't inidicate it has receives any file.
I do test this whole afternoon but to no vail. I copy the source code that people have claim successful executing but it doesn't work for me. There is no detail error code so I am not able to figure what is wrong.
Any suggestion or method is welcomed.
Copied from original issue: NativeScript/nativescript-angular#770
From @samliaw on April 25, 2017 14:6
Hi, I am testing nativescript-background-http to test upload a picture file to server.
I test using physical mobile device. Below is the coding file ==================================start of a.component.ts================================
==================================end of a.component.ts================================
==================================start of package.json================================
==================================end of package.json================================
I am using the node.js restify to accept the file. I have tested using Postman and it works file. The code of the restify is as below ==================================start of restify index.js================================
==================================end of restify index================================
==================================start of restify packson.json================================
==================================endof restify packson.json================================
When I use the Postman, I provide the following attirbutes Method: Post URL: http://it-enable.net:9191/file Header: {"Content-Type":"application/octet-stream"} Attached a picture file.
If the file is uploaded successfully, I can see the response of "Done!".
When I run the project in physical mobile device, I hit the error "error during upload." When I look at backend restify, I find that the nativescript-background-http hits error before it sends the file to backend restify. This is because the backend restify doesn't inidicate it has receives any file.
I do test this whole afternoon but to no vail. I copy the source code that people have claim successful executing but it doesn't work for me. There is no detail error code so I am not able to figure what is wrong.
Any suggestion or method is welcomed.
Copied from original issue: NativeScript/nativescript-angular#770