菜鸟教程小白 发表于 2022-12-12 21:30:27

ios - 将照片从 iPhone 上传到 4D 服务器


                                            <p><p>我希望将照片从 iPhone 上传到 4D 服务器。专门用于上传在 iPhone 上拍摄的照片,上传到 4D 服务器并作为 JPEG 图像存储在 WebFolder 中。我正在使用 4D Server 版本 12 和 13。我在这里查看了其他帖子,但我无法将它们中的任何一个应用于 4D。有人知道怎么做吗?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我花了一段时间,但最终我将各种程序组合在一起以完成这项工作。竖起大拇指 iriphon 博客 (http://www.iriphon.com/2011/11/09/ios-uploading-an-image-from-your-iphone-to-a-server/) 把我放在右边跟踪。</p>

<p>这是我使用的 iOS 代码:</p>

<pre><code>-(IBAction)uploadPhoto:(id)sender {
NSMutableURLRequest *request = [ initWithURL:
                                                            cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                      timeoutInterval:60];

;

// We need to add a header field named Content-Type with a value that tells that it&#39;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 = @&#34;----WebKitFormBoundaryQkZe6RUJZ2xbzXLB&#34;;

NSString *contentType = ;

;
// end of what we&#39;ve added to the header.

// the body of the post.
NSMutableData *body = ;

// Now we need to append the different data &#39;segments&#39;. We first start by adding the boundary.
dataUsingEncoding:NSUTF8StringEncoding]];

// Now append the image
// Note that the name of the form field is exactly the same as in the trace (&#39;picBLOB&#39; in my case).
// You can choose whatever filename you want.
dataUsingEncoding:NSUTF8StringEncoding]];

// We now need to tell the receiver what content type we have.
// In my case it&#39;s a jpg image. If you have a png, set it to &#39;image/png&#39;.
dataUsingEncoding:NSUTF8StringEncoding]];

// Now we append the actual image data.
// I use a NSData object called photoData.
];

NSLog(@&#34;photoData size = %i&#34;,);

// and again the delimiting boundary.
dataUsingEncoding:NSUTF8StringEncoding]];

// adding the body we&#39;ve created to the request.
;

NSURLConnection * connection = [ initWithRequest:request delegate:self];
NSLog(@&#34;connection = %@&#34;,connection);
}
</code></pre>

<p>不要忘记包含 NSURLConnection 代表!</p>

<p>如博客中所述,我使用 Wireshark 来获取我需要的确切数据。我正在使用 NSData 存储我刚刚用相机拍摄并想要上传的照片。因此,我的代码与您要上传存储的文件时略有不同。</p>

<p>这是 4D(版本 12)代码:</p>

<pre><code>$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)&gt;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(&#34;filename=&#34;;$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(&#34;Content-Type:&#34;;$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;&#34;JPEG&#34;)
$FILENAME:=&#34;:WebFolder:myPhoto.jpg&#34;
WRITE PICTURE FILE($FILENAME;imageVar;&#34;JPEG&#34;)

End if
</code></pre>

<p>这是 4D(版本 13)代码:</p>

<pre><code>C_BLOB(picBLOB)
C_PICTURE(imageVar)

WEB GET BODY PART(1;picBLOB;myPhoto)

If (BLOB size(picBLOB)&gt;0)
BLOB TO PICTURE(picBLOB;imageVar;&#34;JPEG&#34;)
$FILENAME:=&#34;:WebFolder:myPhoto.jpg&#34;
WRITE PICTURE FILE($FILENAME;imageVar;&#34;JPEG&#34;)
End if
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 将照片从 iPhone 上传到 4D 服务器,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/11127779/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/11127779/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 将照片从 iPhone 上传到 4D 服务器