简体   繁体   English

如何判断文件是否已通过NSFileManager ios上传

[英]How to tell if a file has been uploaded through NSFileManager ios

I'm uploading files to Dropbox, and I'm wondering if I can mark anything through NSFileManager to test to see whether a file has already been uploaded. 我正在将文件上传到Dropbox,我想知道我是否可以通过NSFileManager标记任何内容来测试是否已经上传文件。 I've been combing through the documentation and haven't found anything yet that could help. 我一直在梳理文档,还没有找到任何可以帮助的东西。

So for instance, if I've uploaded a file called song.m4a, and the user changes the name of that file in the app, how would I be able to find out whether that file has been uploaded with the new name so that the file doesn't get uploaded again? 例如,如果我上传了一个名为song.m4a的文件,并且用户在应用程序中更改了该文件的名称,我怎么能够找出该文件是否已使用新名称上传,以便文件没有再次上传?

Are there any properties or attributes I could set to see if the file has been uploaded? 是否有任何属性或属性我可以设置以查看文件是否已上传?

Thanks. 谢谢。

You can use hashing, eg you can calculate the MD5 hash of the file and store it in a local file on the phone, when the user tries to upload a file, you don't check its name, you simply recalculate the MD5 hash and check if it exists in your local file, if it does, then it was uploaded once before. 您可以使用散列,例如,您可以计算文件的MD5散列并将其存储在手机上的本地文件中,当用户尝试上传文件时,您不检查其名称,只需重新计算MD5散列和检查它是否存在于您的本地文件中,如果存在,则之前上传过一次。

Edit: 编辑:

You can convert anything to NSData and then calculate the hash of that NSData, eg in your case you can load the file like this 您可以将任何内容转换为NSData,然后计算该NSData的哈希值,例如,在您的情况下,您可以像这样加载文件

NSData* data = [NSData dataWithContentsOfFile:yourFilePath];

then you can hash it like this 然后你可以像这样哈希吧

- (NSString*)MD5:(NSData*)input
{
  // Create byte array of unsigned chars
  unsigned char md5Buffer[CC_MD5_DIGEST_LENGTH];

  // Create 16 byte MD5 hash value, store in buffer
  CC_MD5(input.bytes, input.length, md5Buffer);

  // Convert unsigned char buffer to NSString of hex values
  NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
  for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) 
    [output appendFormat:@"%02x",md5Buffer[i]];

  return output;
}

and don't forget to import 并且不要忘记导入

#import <CommonCrypto/CommonDigest.h>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM