从iPhone上传图像到服务器(ASP.Net)

本文关键字:ASP Net 服务器 iPhone 图像 | 更新日期: 2023-09-27 18:15:29

我正在尝试使用ASP.Net从iPhone上传到远程服务器。但我不能让它工作。事情变得更奇怪了,因为这段代码两天前还在运行。WTF ?

这是ASP。Net代码:

public partial class UploadPhoto : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            HttpPostedFile file = Request.Files.Count > 0 ? Request.Files[0] : null;
            String filename = System.Guid.NewGuid().ToString("N");
            String[] names = file.FileName.Split('.');
            string src = filename + "." + names[names.Length - 1];
            file.SaveAs(Server.MapPath("~/Uploads/") + src);
            Response.Write("Success");
            return;
        }
        catch (Exception)
        {
            Response.Output.Write("Failure");
        }
    }
}

这是Objective C代码:

-(IBAction)UploadPhoto:(id)sender{ 
    NSData *imageData = UIImagePNGRepresentation(ImageView.image); 
    NSString *urlString = [NSString stringWithFormat:@"%@UploadPhoto.aspx", URL]; 

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
    [request setURL:[NSURL URLWithString:urlString]]; 
    [request setCachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData]; 
    [request setHTTPShouldHandleCookies:NO]; 
    [request setHTTPMethod:@"POST"]; 
    NSString *boundary = @"---------------------------14737809831466499882746641449"; 
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; 
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; 
    NSMutableData *body = [NSMutableData data]; 
    [body appendData:[[NSString stringWithFormat:@"'r'n--%@'r'n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name='"userfile'"; filename='"image.png'"'r'n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[[NSString stringWithFormat:@"Content-Type: application/octet-stream'r'n'r'n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [body appendData:[NSData dataWithData:imageData]]; 
    [body appendData:[[NSString stringWithFormat:@"'r'n--%@--'r'n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
    [request setHTTPBody:body]; 
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 
    NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; 
    NSLog(@"%@", returnString); 
} 

有谁知道为什么这不起作用吗?我已经查看了其他代码片段,从字面上复制/粘贴代码,但仍然没有。

从iPhone上传图像到服务器(ASP.Net)

儿子....

一直是服务器。

页面请求将重定向导致所有POST数据消失。

我所要做的就是删除"。

链接: http://iphonedevsdk.com/forum/iphone-sdk-development/26394-cannot-http-post-to-server-returns-empty-post.html