我有一個從 url 下載 pdf 的代碼..但是每次我在按鈕上下載 url 時,單擊它都會用新下載的同名 pdf 替換舊 pdf。
這是我的代碼:
NSString *stringURL = @"https://www.clickdimensions.com/links/TestPDFfile.pdf";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"filename.pdf"];
[urlData writeToFile:filePath atomically:YES];
}
- 我想在手機locel下載檔案夾中下載pdf
- 當用戶再次單擊下載 pdf 按鈕時,應下載同一個 pdf,名稱為 filename(2).pdf 它不應替換第一個
uj5u.com熱心網友回復:
我在想類似的東西:
NSString *filePath = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"filename.pdf"];
int fileCopyNum = 1;
// Loop until available file name is found
while ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
// compute a new file name by incrementing index
// first new file name will start at 1 = 2
fileCopyNum ;
filePath = [NSString stringWithFormat:@"%@/%@-(%d).pdf", documentsDirectory,@"filename", fileCopyNum];
}
[urlData writeToFile:filePath atomically:YES];
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/470156.html