为什么文件选择器中的.lastPathComponent返回奇怪的文件名?

时间:2020-10-12 05:33:56

标签: ios swift file

我正在尝试从文件选择器中获取真实的文件名。我使用了两种方法,例如:

let fileName = file.lastPathComponent

等等:

let fileName = FileManager.default.displayName(atPath: file.absoluteString)

其中filelet file = urls[0]。在任何时候我都会收到奇怪的信件集:

%D0%91%D0%B5%D0%B7%20%D0%BD%D0%B0%D0%B7%D0%B2%D0%B0%D0%BD%D0%B8%D1%8F.rtf

真实文件名为:Без названия.rtf。真正的文件名是俄语,当我尝试获取英语文件名的文件时,一切都很好,所以我发现所有问题都与俄语文件名有关。也许有人遇到类似的问题并且知道如何解决?我也尝试过utf-8编码,但对我没有帮助:(

更新

我通过websocket任务将其发送到聊天服务器:

func documentPicker(_ controller: UIDocumentPickerViewController, 

    didPickDocumentsAt urls: [URL]) {
            
            let file = urls[0]
            do{
                let fileData = try Data.init(contentsOf: file)
                let encodedString = String.init(data: fileData, encoding: .isoLatin1)!
                
                let fileName = FileManager.default.displayName(atPath: file.path)
                let time = Int64(NSDate().timeIntervalSince1970)
                
                
                print(encodedString.count)
                wsSendString(message: ChatMStrings.init().sendFilePart(fileContent: encodedString, fileName: fileName, fileSize: encodedString.count))
                
            }catch{
                print("contents could not be loaded")
            }
        }

发送对象:

func sendFilePart(fileContent: String, fileName: String, fileSize: Int) -> String {
        
        let sendFile:[String:[String:Any]] = ["chat":["a":"send_file",
                                                      "body":fileContent,
                                                      "filename":fileName,
                                                      "total":fileSize]]
        
        let jsonData = try? JSONSerialization.data(withJSONObject: sendFile, options: [])
        return String(data: jsonData!, encoding: String.Encoding.ascii)!
    }

发送方法:

func wsSendString(message:String) {
        self.webSocket!.send(URLSessionWebSocketTask.Message.string(message)) { error in
            if let error = error {
                print("WebSocket sending error: \(error)")
            }
        }
        
        
        self.listenWS()
    }

websocket创建:

 webSocket = urlSession.webSocketTask(with: request)
webSocket!.resume()

1 个答案:

答案 0 :(得分:2)

存在的问题是您使用了错误的URL属性。 file.absoluteString错误。您应该使用file.path。

let fileURL = URL(fileURLWithPath: "Без названия.rtf")
print(fileURL) // %D0%91%D0%B5%D0%B7%20%D0%BD%D0%B0%D0%B7%D0%B2%D0%B0%D0%BD%D0%B8%D1%8F.rtf -- file:///private/var/folders/00/l311vw5s2550g5gz2h25b2vr0000gp/T/
print(fileURL.path)  // /private/var/folders/00/l311vw5s2550g5gz2h25b2vr0000gp/T/com.apple.dt.Xcode.pg/containers/com.apple.dt.playground.stub.iOS_Simulator.MyPlayground-comparable-enumeration-AD2BDA8A-BF7B-4F92-B561-B080E72B4DF0/Без названия.rtf
print(fileURL.absoluteString)  // file:///private/var/folders/00/l311vw5s2550g5gz2h25b2vr0000gp/T/com.apple.dt.Xcode.pg/containers/com.apple.dt.playground.stub.iOS_Simulator.MyPlayground-comparable-enumeration-AD2BDA8A-BF7B-4F92-B561-B080E72B4DF0/%D0%91%D0%B5%D0%B7%20%D0%BD%D0%B0%D0%B7%D0%B2%D0%B0%D0%BD%D0%B8%D1%8F.rtf
let fileNamePath = FileManager.default.displayName(atPath: fileURL.path)
print(fileNamePath) // "Без названия.rtf\n"
let fileNameAbsoluteString = FileManager.default.displayName(atPath: fileURL.absoluteString)
print(fileNameAbsoluteString)  // %D0%91%D0%B5%D0%B7%20%D0%BD%D0%B0%D0%B7%D0%B2%D0%B0%D0%BD%D0%B8%D1%8F.rtf