0

I am sending image from my iPhone App to server. Restful API is in express JS. I am getting image on server using POST request. I am able to print the binary data but not able to store it properly. When I open the image from specified location,its corrupted. Here is my express js code.

app.post('/api/pictures',function(req,res){ console.log(req.headers); var body = ''; filePath = 'C:/Users/Desktop/restful/image.png'; req.on('data', function(data) { console.log(data); body += data; }); req.on('end', function (){ fs.appendFile(filePath, body, function() { res.end(); }); }); res.send("Success!"); }); 

My req.headers is

enter image description here

Am I doing anything wrong?

Where as I am getting this response on client side

{ status code: 200, headers { Connection = "keep-alive"; "Content-Length" = 8; "Content-Type" = "text/html; charset=utf-8"; Date = "Wed, 14 Jan 2015 16:15:01 GMT"; "X-Powered-By" = Express; } } upload completed, response: Optional(Success!) 

Thanks in advance.

2 Answers 2

1

Finally I am able to upload image using connect-multiparty.

app.post('/api/uploadimage', multipartMiddleware, function(req, res) { console.log(req.body, req.files); // check console fs.readFile(req.files.urForm-data_name.path, function (err, data) { fs.writeFile(newPath, data, function (err) { }); }); }); 
Sign up to request clarification or add additional context in comments.

Comments

0

You need to use a multipart-parsing middleware such as multer, multiparty, formidable, or connect-busboy.

3 Comments

can you please give me any sample code or tutorial link?
I searched for sample code of multiparty here github.com/andrewrk/node-multiparty But didn't find any REST API related sample code or tutorial. What I found is all related to post images from an html form. But I am sending image from the iPhone app. I am very new to javascript. Can you please help me with this? I am really stuck here.
I am able to get parameters which I am sending from app. Also getting image size. How to retrieve this image data from part? app.post('/api/uploadimage', function(req, res, next){ var form = new multiparty.Form(); form.on('part', function(part) { if (part.filename) { var filename = part.name; var size = part.byteCount; console.log("filename = "+filename); } }); form.parse(req); });

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.