将 ASIHTTPRequest 发布到 SimpleHTTPServer Python 服务器?
我正在开发一个项目(我不会发布到应用程序商店 - 只是为了好玩),该项目将通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如@SorinA。提到,您应该提供一个文件路径作为
setFile:withFileName:andContentType:forKey:
的第一个参数。在您的示例中,您将提供一个UIImage
对象。然而,最简单的解决方法可能是仅使用 setData:withFileName:andContentType:forKey: 。您只需将
UIImage
对象转换为其NSData
表示形式:另外,您的 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 aUIImage
object instead.The simplest fix, however, might be to just use
setData:withFileName:andContentType:forKey:
. You just need to convert yourUIImage
object to itsNSData
representation: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.
顺便说一句,我没有看到明显的问题,但我会尝试调用 [request responseStatusCode] 和 [request responseStatusMessage] 而不是 [request responseString]。我猜对帖子的回复不会有字符串。我认为它应该返回 200 或类似的值来表明成功。
您也可以尝试:
最后..我会查看 ASIHTTPRequestConfig.h 我也使用 ASI,所以我只是看了看它,它有一些应该有所帮助的调试选项。
将 0 设置为 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:
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:
If you haven't tried those, that should give you some info.
您尝试过使用 ASIFormDataRequest 吗?
Have you tried to use ASIFormDataRequest?
在您的代码中,为什么在同一请求上使用 setData 和 setFile ?如果您要发送大文件,您应该使用“setFile”而不是“setData”。然后从磁盘而不是内存中读取文件。
当您设置内容类型时,您应该设置正确的值。
内容类型应为 image/png 。
要上传图像,我使用以下代码:
此外,如果您一次上传多张照片,请使用 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.
content type should be image/png .
to upload an image i use this code:
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.