I try to submit images to my server.
In the Objective-C part, I append the image data to the HTTP body, using "form-data" with "name" and "filename". Below is the relevant snippet. I'm pretty sure other parts should be correct. Please focus on the line with the "Content-Disposition" if necessary:
...
// Append image data to body data
NSMutableData *bodyData = [NSMutableData data];
int i = 1;
for (NSData *imgData in allImgData) {
[bodyData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"photoToUpload[]\"; filename=\"%@%d.png\"\r\n", fileParamConstant, i] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:[@"Content-Type: image/png\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[bodyData appendData:imgData];
[bodyData appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
i++;
}
// Append one last boundary
[bodyData appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[urlRequest setHTTPBody:bodyData];
...
Problem
However, after searching several posts online, I'm still not so sure how I should handle the server part with nodejs using mongoose. Based on my Objective-C part, should I get the image from "req.body" or "req.files"? So I think my problem also lies in understanding the HTTP request, especially related to the "Content-Disposition" part above.
Below is my current nodejs code:
// on routes that end in /users/competitorAnalysisPhotoData
// ----------------------------------------------------
router.route('/users/competitorAnalysisPhotoData/:userName')
// update the user info (accessed at PUT http://localhost:8080/api/users/competitorAnalysisPhotoData)
.put(function(req, res) {
// use our user model to find the user we want
User.findOne({ userName: req.params.userName}, function(err, user) {
if (err)
res.send(err);
console.log('Got the user in "Put"!');
// update the photos -- Version 1
user.competitorAnalysis.push({
photo1: req.body.photo1,
photo2: req.body.photo2,
photo3: req.body.photo3,
photo4: req.body.photo4
});
console.log('req.body.photo1: %s', req.body.photo1);
console.log('Save the photo data for competitorAnalysisPhotoData!');
// update the photos -- Version 2
user.competitorAnalysis.photo1.data = fs.readFileSync(req.files.photo1.path)
user.competitorAnalysis.photo1.contentType = 'image/png';
user.competitorAnalysis.photo2.data = fs.readFileSync(req.files.photo2.path)
user.competitorAnalysis.photo2.contentType = 'image/png';
user.competitorAnalysis.photo3.data = fs.readFileSync(req.files.photo3.path)
user.competitorAnalysis.photo3.contentType = 'image/png';
user.competitorAnalysis.photo4.data = fs.readFileSync(req.files.photo4.path)
user.competitorAnalysis.photo4.contentType = 'image/png';
console.log('req.files.photo1.path: %s', req.files.photo1.path);
console.log('Save the photo data for competitorAnalysisPhotoData!');
// save the user
user.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'User updated!' });
});
});
It will be great if I can get some suggestions on how I should deal with the server-side properly.