我试过几种方法将文件从swift上传到FTP服务器。我只看到文件的内容是传输而不是完整的文件。如何传输整个文件而不是内容?以下是我的代码,
let pat = (UserDefaults.standard.object(forKey: "mainurl") as? String)
let path11 = self.localFilePathForUrl1(previewUrl: pat!)
let data: NSData = NSData(contentsOfFile: path11!)!
let request = NSMutableURLRequest(url: NSURL(string:(UserDefaults.standard.object(forKey: "mainurl") as? String)!)! as URL)
request.httpMethod = "POST"
let boundary = generateBoundaryString()
request.setValue("myagent", forHTTPHeaderField:"User-Agent")
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
let body = NSMutableData()
body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Disposition:form-data; name=file; filename=\(filename!)\"\r\n\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Type: \(mimetype)\r\n\r\n".data(using: String.Encoding.utf8)!)
body.append(data as Data)
body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
task.resume()
服务器可以接受标题和附加参数,但我看到(使用Fiddler
)传输文件的内容而不是完整的文件传输。