从iPhone上传照片到4D服务器
我期待从iPhone上传照片到4D服务器。具体来说,要上传在iPhone上拍摄的照片,并将其上传到4D服务器并作为JPEG图像存储在WebFolder中。我正在使用4D服务器版本12和13.我已经看过其他帖子,但是我不能让它们适用于4D。有人知道怎么做吗?从iPhone上传照片到4D服务器
我花了一段时间,但我终于困惑在一起的各种程序,使这项工作。竖起大拇指向iriphon博客(http://www.iriphon.com/2011/11/09/ios-uploading-an-image-from-your-iphone-to-a-server/)让我在右边跟踪。
这里是iOS的代码,我用:
-(IBAction)uploadPhoto:(id)sender {
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.domain.com/4DACTION/showcaseUploadPic"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60];
[request setHTTPMethod:@"POST"];
// We need to add a header field named Content-Type with a value that tells that it's a form and also add a boundary.
// I just picked a boundary by using one from a previous trace, you can just copy/paste from the traces.
NSString *boundary = @"----WebKitFormBoundaryQkZe6RUJZ2xbzXLB";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
// end of what we've added to the header.
// the body of the post.
NSMutableData *body = [NSMutableData data];
// Now we need to append the different data 'segments'. We first start by adding the boundary.
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// Now append the image
// Note that the name of the form field is exactly the same as in the trace ('picBLOB' in my case).
// You can choose whatever filename you want.
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"picBLOB\" filename=\"image001.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
// We now need to tell the receiver what content type we have.
// In my case it's a jpg image. If you have a png, set it to 'image/png'.
[body appendData:[[NSString stringWithString:@"Content-Type: image/jpg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
// Now we append the actual image data.
// I use a NSData object called photoData.
[body appendData:[NSData dataWithData:photoData]];
NSLog(@"photoData size = %i",[photoData length]);
// and again the delimiting boundary.
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// adding the body we've created to the request.
[request setHTTPBody:body];
NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
NSLog(@"connection = %@",connection);
}
不要忘了包括NSURLConnection的代表!
我用博客中描述的Wireshark来获取我需要的确切数据。我正在使用NSData存储刚刚用相机拍摄的照片并想上传。正因为如此,我的代码与上传存储文件的方式略有不同。
这里是4D(12版)代码:
$ba:=BLOB size(picBLOB)
C_PICTURE(imageVar)
If (Undefined(picBLOB)) // in case user tries to reload /4DACTION/WEB_Set_Limits
C_BLOB(picBLOB)
End if
If (BLOB size(picBLOB)>0) // did user select a file for uploading?
C_STRING(255;$fileHeader)
$offset:=0 // BLOB to text requires a variable for the offset parameter
$fileHeader:=BLOB to text(picBLOB;Mac text without length;$offset;255)
$fileNameBegin:=Position("filename=";$fileHeader)+10
For ($i;$fileNameBegin;255)
If ($fileHeader≤$i≥=Char(Double quote))
$fileNameEnd:=$i
$i:=255
End if
End for
$fileName:=Substring($fileHeader;$fileNameBegin;$fileNameEnd-$fileNameBegin)
$contentTypeBegin:=Position("Content-Type:";$fileHeader)+14
For ($i;$contentTypeBegin;255)
If ($fileHeader≤$i≥=Char(Carriage return))
$contentTypeEnd:=$i
$i:=255
End if
End for
contentType:=Substring($fileHeader;$contentTypeBegin;$contentTypeEnd-$contentTypeBegin)
DELETE FROM BLOB(picBLOB;0;$contentTypeEnd+3)
BLOB TO PICTURE(picBLOB;imageVar;"JPEG")
$FILENAME:=":WebFolder:myPhoto.jpg"
WRITE PICTURE FILE($FILENAME;imageVar;"JPEG")
End if
这里是4D(版本13)代码:
C_BLOB(picBLOB)
C_PICTURE(imageVar)
WEB GET BODY PART(1;picBLOB;myPhoto)
If (BLOB size(picBLOB)>0)
BLOB TO PICTURE(picBLOB;imageVar;"JPEG")
$FILENAME:=":WebFolder:myPhoto.jpg"
WRITE PICTURE FILE($FILENAME;imageVar;"JPEG")
End if
我对这个“4D服务器”不是特别熟悉,但听起来你只需要一个简单的服务器端PHP脚本来上传照片,可能使用NSData。
你究竟试过了什么,或者你在这两方面的知识程度如何?
我试图http://stackoverflow.com/questions/125306/how-can-i-upload-a-photo-to-a-server-with-the-iphone和我试图不使用ASIHttpRequest,因为它是更长的支持。 PHP在4D中不是本地的,我自己也不熟悉PHP。 FYI 4D在www.4d.com – sangony
此page有使用早期版本的说明。 iNug上的more recent post(4D的开发邮件列表)表明,这仍然有效,也许有一些推文。
仅仅因为4D社区太小,您会在iNug上得到更好的回复。
感谢您对iNug的建议。 – sangony
是你的问题与iOS的一侧或4D的一面呢? –
使用4DACTION方法,问题将出现在4D端,如何使用类似NSURL的方式接收iPhone发送的数据。我相信还有其他方法,但目前我不知道他们。 – sangony