【问题标题】:Upload photo from iPhone to 4D Server将照片从 iPhone 上传到 4D 服务器
【发布时间】:2012-06-23 01:48:01
【问题描述】:

我希望将照片从 iPhone 上传到 4D 服务器。专门用于上传在 iPhone 上拍摄的照片,上传到 4D 服务器并作为 JPEG 图像存储在 WebFolder 中。我正在使用 4D Server 版本 12 和 13。我在这里查看了其他帖子,但我无法将它们中的任何一个应用于 4D。有人知道怎么做吗?

【问题讨论】:

  • 你的问题是iOS端还是4D端?
  • 使用 4DACTION 方法,问题将出现在 4D 方面,即如何使用 NSURL 之类的东西接收 iPhone 发送的数据。我相信还有其他方法,但我目前不知道。

标签: ios web-services 4d-database


【解决方案1】:

我花了一些时间,但最终我将各种程序组合在一起以完成这项工作。竖起大拇指 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 

【讨论】:

    【解决方案2】:

    我对这个“4D 服务器”不是特别熟悉,但听起来你只需要一个简单的服务器端 PHP 脚本来上传照片,可能使用 NSData。

    您究竟尝试过什么,或者您在这两个领域的知识程度如何?

    【讨论】:

    • 我尝试了stackoverflow.com/questions/125306/…,但我试图不使用 ASIHttpRequest,因为它不再受支持。 PHP 不是 4D 原生的,我自己也不是很精通 PHP。仅供参考 4D 位于 www.4d.com
    【解决方案3】:

    page 包含使用早期版本的说明。 iNug(4D 的开发邮件列表)上的 more recent post 表明这仍然有效,可能需要几个星期。

    您在iNug 上提问会得到更好的回应,这仅仅是因为 4D 社区太小了。

    【讨论】:

      猜你喜欢
      • 2012-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-12
      • 2011-04-11
      • 2012-02-19
      相关资源
      最近更新 更多