简体   繁体   English

使用Swift保存其他名称的文件

[英]Save file with different name using Swift

I am using Alamofire to download files from the server. 我正在使用Alamofire从服务器下载文件。 But i want to name it differently just like how we download the file from web and can name it as we want, same How is it possible in swift 但是我想用不同的方式命名它,就像我们从网络上下载文件一样,并可以根据需要命名,同样,如何迅速

I am using the following code:- 我正在使用以下代码:-

Alamofire.download(fileUrl, to: destination)
        .downloadProgress { progress in
            print("Download Progress:---------- \   
        (progress.fractionCompleted)")


        }
        .responseData { response in

            print(response.request)
            print(response.response)
            print(response.temporaryURL)
            print(response.destinationURL)
            print(response.error)

    }

Prefering not to use alamofire to rename or overrite files. 最好不要使用alamofire重命名或覆盖文件。 Is there any other method 还有其他方法吗

Here is the hands on function to save your Data into DocumentDirectory with the name you given. 这是动手操作的功能,可以使用您提供的名称将Data保存到DocumentDirectory中 This method have one call back which will call after performing saving action, it will return the path where the image saved in directory. 此方法有一个call back ,该call back将在执行保存操作后调用,它将返回图像保存在目录中的路径。 This function written in Swift 3 . 这个函数用Swift 3编写。

func saveImageInDocDirectory(data: Data?, fileName: String?, successblock: @escaping (_ path: String?) -> Void) { // To add the image to cache for given identifier.

    let paths = NSSearchPathForDirectoriesInDomains( .documentDirectory, .userDomainMask, true)[0] as String
    let path = paths.appending("/\(fileName!)")
    if !FileManager.default.fileExists(atPath: path) {
        do {
            try FileManager.default.createDirectory(at: URL(fileURLWithPath: path), withIntermediateDirectories: false, attributes: nil)
        } catch {
            print("Error creating images folder in Cache dir: \(error)")
        }
    }

    if (filemgr.fileExists(atPath: path)) {
        try! filemgr.removeItem(atPath: path)
    } else {
        do {
            try data?.write(to: URL(fileURLWithPath: path, isDirectory: false))
            successblock(path)
        } catch {
            successblock(nil)
            print("Error while caching the data in cache folder.")
        }
    }

}

you can call this method like, 您可以像这样调用此方法

Alamofire.download(fileUrl, to: destination)
    .downloadProgress { progress in
        print("Download Progress:---------- \   
    (progress.fractionCompleted)")


    }
    .responseData { response in

        print(response.request)
        print(response.response)
        print(response.temporaryURL)
        print(response.destinationURL)
        print(response.error)
       if let data = response.result.value {
         self.saveImageInDocDirectory(data: data, fileName: "YourFile",  successblock: { (path) in

                print(path)

            }
       }

}

Thanks. 谢谢。

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

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