iPhone 文件上传

发布于 2024-11-29 00:16:31 字数 326 浏览 2 评论 0原文

我正在构建一个本机 iPhone 应用程序,其中具有文件上传功能。我需要验证并确认文件是否已完全上传。我怎样才能实现这个目标?我的服务器端脚本是用 PHP 构建的,用于接受文件数据并在服务器上写入/创建文件。

所以我正在尝试做两件事: 1.计算来自iPhone的文件的校验和并将其发布到PHP脚本 2. PHP 脚本在服务器上创建文件后,重新计算校验和并将其与发布的值进行比较。

我的查询在这里: 我使用 MD5 来计算校验和,但 Objective C 和 PHP 脚本返回的值不同?

在 iPhone 上计算校验和的最佳方法是什么?可以在服务器端进行匹配。

请建议。谢谢

I am building a native iPhone application where I have the File Upload functionality. I need to verify and confirm if the file was uploaded completely. How can I achieve this? My server side script to accept the file data and write/create the file on the server is built in PHP.

So I am trying to do 2 things:
1. Calculate the checksum of the file from iPhone and post it to PHP script
2. Once the PHP script creates the file on the server, recalculate the checksum and compare it with the posted value.

My Query here:
I am using MD5 to calculate the checksum but the values returned by objective c and PHP script are different?

Which is the best way I can calculate the checksum on iPhone? which can be matched on the server side.

Please suggest. Thanks

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

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

发布评论

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

评论(1

幸福%小乖 2024-12-06 00:16:31

这是我总是用于 md5sums 的代码片段:

    - (NSString *) md5:(NSString *) input
{
    const char *cStr = [input UTF8String];
    unsigned char digest[16];
    CC_MD5( cStr, strlen(cStr), digest ); // This is the md5 call

    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];

    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];

    return  output;

}

在此处查看 ASIFormDataRequest http://allseeing-i .com/ASIHTTPRequest/如何使用
对于此类操作来说,它是一个非常有用的包装器。在我的用于上传文件和 md5sum 的代码片段下方:

ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString: uploadUrl]] autorelease];
    [request setPostValue:md5sum forKey:@"md5sum"];
    [request setFile:[[NSURL URLWithString:filename] path] forKey:@"file"];
    [request startSynchronous];

确保检查服务器端是否一切正常(md5sums 匹配并且文件上传正常)。然后使用 [request responseString] 评估服务器的响应并采取适当的操作。

Here's a code snippet I always use for md5sums:

    - (NSString *) md5:(NSString *) input
{
    const char *cStr = [input UTF8String];
    unsigned char digest[16];
    CC_MD5( cStr, strlen(cStr), digest ); // This is the md5 call

    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];

    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];

    return  output;

}

Check out ASIFormDataRequest here http://allseeing-i.com/ASIHTTPRequest/How-to-use
It's a very helpful wrapper for this kind of operation. Below my codesnippet for uploading a file and md5sum:

ASIFormDataRequest *request = [[[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString: uploadUrl]] autorelease];
    [request setPostValue:md5sum forKey:@"md5sum"];
    [request setFile:[[NSURL URLWithString:filename] path] forKey:@"file"];
    [request startSynchronous];

Make sure that you check serverside if everything is ok (md5sums match and file was uploaded ok). Then evaluate your server's response with [request responseString] and take appropriate action.

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