0

我正在尝试将图像从 iOS App 上传到我的基于 asp.net mvc 的站点。我正在从本地编写此代码,但由于某种原因 HttpPostedFileBase 为空...我找不到任何错误,任何提示为什么?

iOS端:

-(void)Upload
{
    NSLog(@"Upload stuff");

    NSString *urlString = @"http://.... ";

    // set up the form keys and values (revise using 1 NSDictionary at some point - neater than 2 arrays)
    NSArray *keys = [[NSArray alloc] initWithObjects:@"auth",@"text",@"location",nil];
    NSArray *vals = [[NSArray alloc] initWithObjects:@"au",@"txt",@"loc",nil];

    // set up the request object
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];

    //Add content-type to Header. Need to use a string boundary for data uploading.
    NSString *boundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"];
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    //create the post body
    NSMutableData *body = [NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSASCIIStringEncoding]];

    //add (key,value) pairs (no idea why all the \r's and \n's are necessary ... but everyone seems to have them)
    for (int i=0; i<[keys count]; i++) {
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n",[keys objectAtIndex:i]] dataUsingEncoding:NSASCIIStringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"%@",[vals objectAtIndex:i]] dataUsingEncoding:NSASCIIStringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSASCIIStringEncoding]];
    }
    [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"image\"\r\n"] dataUsingEncoding:NSASCIIStringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSASCIIStringEncoding]];

    NSData *imageData = UIImageJPEGRepresentation(self.btnCover.imageView.image, 1.0);


    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSASCIIStringEncoding]];

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

    // make the connection to the web
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

    NSLog(returnString);


    NSLog(@"Upload done");

}

Asp.net MVC 控制器动作端:

    [HttpPost]
    public string PostFile(HttpPostedFileBase name)
    {

        var track = new Tracking();//this is my custom tracking class

        track.LogVisit(".", "start");
        track.LogVisit(".", (name == null).ToString() );
        track.LogVisit(".", name.ContentType);
        track.LogVisit(".", name.FileName);
        track.LogVisit(".", name.InputStream.Length.ToString());
        track.LogVisit(".", name.ContentLength.ToString());
        track.LogVisit(".", "end");

        return "k";
    }
4

0 回答 0