通过iOS应用程序中的node.js将图像上传到服务器

前端之家收集整理的这篇文章主要介绍了通过iOS应用程序中的node.js将图像上传到服务器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发一个iOS应用程序,我使用node.js进行服务器端脚本编写.我在iOS应用程序上将图像上传到服务器时遇到问题.如果我从网页表单上传图片,它工作正常.但是,如果从应用程序端上传它不起作用.
//test file
 h3 Pic Upload
 form(action='/pic_upload',method='post',enctype='multipart/form-data') 
  | user_pic:
  input(type='file',name='user_pic')
  input(type='submit')

 //app.js
 var userlogin =require('./routes/userlogin');
 app.post('/pic_upload',userlogin.picUpload);

//userlogin.js
//picUpload function
exports.picUpload = function(req,res) {
console.log(req.files);  // showing undefined,when called from IOS app
// pic upload script...
});

我尝试从应用程序端发送图像作为数据或文件参数,但它不起作用.如何从app端发送文件参数,以便我可以轻松地将图像上传到服务器?请建议一种解决问题的方法.

解决方法

代码适用于我的应用.试试这个.
NSData *imgData = UIImageJPEGRepresentation(newImg,0.2);

NSString *str = @"displayImage";

    NSString *urlString = @"http://some.url.com/post";

    // create request
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
    [request setHTTPShouldHandleCookies:NO];
    [request setTimeoutInterval:30];
    [request setURL:[NSURL URLWithString:urlString]];

    [request setHTTPMethod:@"POST"];

    NSString *boundary = [NSString stringWithFormat:@"---------------------------14737809831464368775746641449"];

    // set Content-Type in HTTP header
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request setValue:contentType forHTTPHeaderField: @"Content-Type"];

    // post body
    NSMutableData *body = [NSMutableData data];

    // add params (all params are strings)
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"currentEventID\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[@"52344457901000006" dataUsingEncoding:NSUTF8StringEncoding]];


    // add image data
    if (imgData) {
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
       // [body appendData:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"displayImage\"; filename=\"image.jpg\"\r\n"]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.jpg\"\r\n",str] dataUsingEncoding:NSUTF8StringEncoding]];

        [body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:imgData];
        [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    }

    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    // setting the body of the post to the reqeust
    [request setHTTPBody:body];

    // set URL
    [request setURL:[NSURL URLWithString:urlString]];

    NSString *bodyStr = [[NSString alloc]initWithData:body encoding:NSUTF8StringEncoding];
    NSLog(@" %@",bodyStr);

    NSURLConnection *lot_Connection=[[NSURLConnection alloc]initWithRequest:request delegate:self];

    if(lot_Connection)
    {
        webdata = [[NSMutableData alloc] init];

    }

猜你在找的iOS相关文章