将 ASIHTTPRequest 发布到 SimpleHTTPServer Python 服务器?

发布于 2024-10-19 10:11:29 字数 1066 浏览 2 评论 0原文

我正在开发一个项目(我不会发布到应用程序商店 - 只是为了好玩),该项目将通过 HTTP Post 请求从我的 iPhone 将图像上传到我运行 Python 脚本 SimpleHTTPServer(http:// /ubuntuguide.net/http-server-support-uploading-files-from-windows-in-ubuntu)。我过去曾成功地使用 ASIHTTP API 来处理文本字符串,但我一直不知道如何上传图像。这就是我目前正在使用的:

-(void)processRequest {

    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"mySimpleHTTPServer"]]; 
    [request setFile:[UIImage imageNamed:@"Image.png"] withFileName:@"Image.png" andContentType:@"image/png" forKey:@"file"];

    [request startSynchronous]; 
    NSLog(@"%@",[request responseString]);
    NSUserDefaults *profiles = [NSUserDefaults standardUserDefaults];
    [profiles setObject:[request responseString] forKey:@"response"];
    [profiles synchronize];

    [self performSelector:@selector(endRequest) withObject:nil afterDelay:0];

}

真的,我认为我错的唯一部分是:

[request setFile:[UIImage imageNamed:@"Image.png"] withFileName:@"Image.png" andContentType:@"image/png" forKey:@"file"];

对我可能出错的地方有什么想法吗?

I am working on a project (that i will not be releasing to the app store - just for fun) that will upload an image via an HTTP Post request from my iPhone to a server that I have running the Python script SimpleHTTPServer(http://ubuntuguide.net/http-server-support-uploading-files-from-windows-in-ubuntu). I have successfully used the ASIHTTP APIs in the past for text strings, but can't for the life of me figure out how to upload an image. This is what I am currently using:

-(void)processRequest {

    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"mySimpleHTTPServer"]]; 
    [request setFile:[UIImage imageNamed:@"Image.png"] withFileName:@"Image.png" andContentType:@"image/png" forKey:@"file"];

    [request startSynchronous]; 
    NSLog(@"%@",[request responseString]);
    NSUserDefaults *profiles = [NSUserDefaults standardUserDefaults];
    [profiles setObject:[request responseString] forKey:@"response"];
    [profiles synchronize];

    [self performSelector:@selector(endRequest) withObject:nil afterDelay:0];

}

Really, the only part that I am assuming I am wrong about is the :

[request setFile:[UIImage imageNamed:@"Image.png"] withFileName:@"Image.png" andContentType:@"image/png" forKey:@"file"];

Any thoughts on where i could be going wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

很糊涂小朋友 2024-10-26 10:11:29

正如@SorinA。提到,您应该提供一个文件路径作为setFile:withFileName:andContentType:forKey:的第一个参数。在您的示例中,您将提供一个 UIImage 对象。

然而,最简单的解决方法可能是仅使用 setData:withFileName:andContentType:forKey: 。您只需将 UIImage 对象转换为其 NSData 表示形式:

NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:@"Image.png"]);
[request setData: imageData
    withFileName: @"Image.png"
  andContentType: @"image/png"
          forKey: @"file"];

另外,您的 URL 看起来有点奇怪(“mySimpleHTTPServer”),我认为这只是一个占位符URL 格式为“http://localhost:some_port/some_path”?务必确认您的 Python Web 服务器确实看到了该请求。

As @SorinA. mentions, you should supply a file path as the first argument to setFile:withFileName:andContentType:forKey:. In your example, you're supplying a UIImage object instead.

The simplest fix, however, might be to just use setData:withFileName:andContentType:forKey:. You just need to convert your UIImage object to its NSData representation:

NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:@"Image.png"]);
[request setData: imageData
    withFileName: @"Image.png"
  andContentType: @"image/png"
          forKey: @"file"];

Also, your URL looks a bit odd ("mySimpleHTTPServer"), I assume this is just a placeholder for a URL in the form of "http://localhost:some_port/some_path"? Definitely confirm that your Python web server is actually seeing the request.

余生一个溪 2024-10-26 10:11:29

顺便说一句,我没有看到明显的问题,但我会尝试调用 [request responseStatusCode] 和 [request responseStatusMessage] 而不是 [request responseString]。我猜对帖子的回复不会有字符串。我认为它应该返回 200 或类似的值来表明成功。

您也可以尝试:

if(request.error) {
    NSLog("ERROR: %@",[error localizedDescription]);
}

最后..我会查看 ASIHTTPRequestConfig.h 我也使用 ASI,所以我只是看了看它,它有一些应该有所帮助的调试选项。

将 0 设置为 1:

DEBUG_FORM_DATA_REQUEST 1
DEBUG_REQUEST_STATUS 1

如果您还没有尝试过这些,那应该会给您一些信息。

Offhand, I don't see an obvious problem, but I would try calling [request responseStatusCode] and [request responseStatusMessage] instead of [request responseString]. I'm guessing that the the response to a post would not have a string. I think it should return 200 or something like that to indicate success.

You can also try:

if(request.error) {
    NSLog("ERROR: %@",[error localizedDescription]);
}

Lastly.. I would look at ASIHTTPRequestConfig.h I'm using ASI as well, so I just peeked at it and it has some debug options that should help.

set the 0's to 1s:

DEBUG_FORM_DATA_REQUEST 1
DEBUG_REQUEST_STATUS 1

If you haven't tried those, that should give you some info.

摇划花蜜的午后 2024-10-26 10:11:29

您尝试过使用 ASIFormDataRequest 吗?

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addData:imageData withFileName:@"mypicture.png" andContentType:@"image/png" forKey:@"photos"];

Have you tried to use ASIFormDataRequest?

ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addData:imageData withFileName:@"mypicture.png" andContentType:@"image/png" forKey:@"photos"];
尹雨沫 2024-10-26 10:11:29

在您的代码中,为什么在同一请求上使用 setData 和 setFile ?如果您要发送大文件,您应该使用“setFile”而不是“setData”。然后从磁盘而不是内存中读取文件。

当您设置内容类型时,您应该设置正确的值。

[请求设置文件:路径
withFileName:@"Image.png"
andContentType:@"image/jpeg"
forKey:@"文件"];

内容类型应为 image/png 。

要上传图像,我使用以下代码:

// Initilize Queue
[networkQueue setUploadProgressDelegate:statusProgressView];
[networkQueue setRequestDidFinishSelector:@selector(imageRequestDidFinish:)];
[networkQueue setQueueDidFinishSelector:@selector(imageQueueDidFinish:)];
[networkQueue setRequestDidFailSelector:@selector(requestDidFail:)];
[networkQueue setShowAccurateProgress:true];
[networkQueue setDelegate:self];

 NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"myImage.jpg"];
 url = [NSURL URLWithString:@"http://myserver/upload.php"];
 ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
 [request setPostValue:@"myImageName" forKey:@"name"]; 
 [request setFile:imagePath forKey:@"photo"];

 [networkQueue addOperation:request];
 [networkQueue go];

此外,如果您一次上传多张照片,请使用 setFile:forKey: 而不是 setData:forKey:。这样,ASIFormDataRequest 将知道您正在发送相当多的数据,并且它将从磁盘上的临时文件传输请求数据,这意味着您不需要在内存中保留大量图像数据。

in your code why do you use setData and setFile on the same request? If you are sending large files you should use “setFile” instead of “setData”. Files are then read from disk instead of memory.

when you are setting content type you should set the correct values.

[request setFile:path
withFileName:@"Image.png"
andContentType:@"image/jpeg"
forKey:@"file"];

content type should be image/png .

to upload an image i use this code:

// Initilize Queue
[networkQueue setUploadProgressDelegate:statusProgressView];
[networkQueue setRequestDidFinishSelector:@selector(imageRequestDidFinish:)];
[networkQueue setQueueDidFinishSelector:@selector(imageQueueDidFinish:)];
[networkQueue setRequestDidFailSelector:@selector(requestDidFail:)];
[networkQueue setShowAccurateProgress:true];
[networkQueue setDelegate:self];

 NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"myImage.jpg"];
 url = [NSURL URLWithString:@"http://myserver/upload.php"];
 ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:url] autorelease];
 [request setPostValue:@"myImageName" forKey:@"name"]; 
 [request setFile:imagePath forKey:@"photo"];

 [networkQueue addOperation:request];
 [networkQueue go];

Also, if you are uploading several photos at once, use setFile:forKey: rather than setData:forKey:. This way, ASIFormDataRequest will know you’re sending a fair bit of data, and it will stream the request data from a temporary file on disk, which means you don’t need to keep lots of image data in memory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文