使用 Swift 4 将 Pdf、Docx 和图像文件上传到服务器



我是 swift 的新手,我一直在尝试从 iPhone 的本地存储上传 pdf、docx 和图像文件。我已经编写了一个代码,但它不起作用,我不断从响应中得到状态代码:415。这是我的代码:

func uploadfileToServer(completed: @escaping () -> ()){
    let theTitle = labelTitle.text

    guard let url = URL(string: "http://www.--------.com/assignment/post") else {return}
    var request = URLRequest.init(url: url)
    request.httpMethod = "POST"
    request.addValue("cf7ab8c9d4efae82b575eabd6bec76cbb86c6108391e036387f3dd5356a582171519367747000", forHTTPHeaderField: "api_key")


    let boundary = generateBoundaryString()

    // Set Content-Type in HTTP header.
    let boundaryConstant = boundary // This should be auto-generated.
    let contentType = "multipart/form-data; boundary=" + boundaryConstant
    let directory = NSTemporaryDirectory()
    let fileName = NSUUID().uuidString
    // This returns a URL? even though it is an NSURL class method
    let fullURL = NSURL.fileURL(withPathComponents: [directory, fileName])

    let fileNamee = fullURL?.path
    let mimeType = "text/csv"
    let fieldName = "uploadFile"
    request.setValue(contentType, forHTTPHeaderField: "Content-Type")

    var dataString = "--(boundaryConstant)rn"

    dataString += "rn"
    dataString += "--(boundaryConstant)--rn"
    var theBody = Data()

    let sectionID : String?
    sectionID = nil
    let str = "user_id=(savedsesuid!)" + "&school_id=" + SCHOOL_ID + "&class_id=" + classID + "&section_id=(sectionID)" + "&subject_id=(id)"

    if let b = str.data(using: .utf8) {
        theBody.append(b)
    }
    let str1 = "&atitle=" + theTitle! + "&class_id=" + classID
    if let c = str1.data(using: .utf8){
        theBody.append(c)
    }

    let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
    var filePath = documentDirectory.appendingFormat("/")
    filePath     = filePath.appendingFormat("/Users/prashanna/Desktop/ebusiness_topic_1_handout.pdf")
    let pdfData  = NSData(contentsOfFile: filePath)
    let file = "&afile=" + "(pdfData)"
    if let d = file.data(using: .utf8){
        theBody.append(d)
    }
    print(theBody)

    request.httpBody = theBody
    URLSession.shared.dataTask(with: request) { (data, response, error) in
        print(response)
        if let httpResponse = response as? HTTPURLResponse {
            let statuscode = httpResponse.statusCode
            if statuscode == 401{
                self.displayMessage(userMessage: "Sending Failed")

            }else if statuscode == 200{
                if error == nil{
                    do{
                        self.displayMessage(userMessage: "File Successfully Uploaded!")
                        DispatchQueue.main.async {
                            completed()
                        }
                    }
                }
            }
        }
    }.resume()

}
func generateBoundaryString() -> String {
    return "Boundary-(NSUUID().uuidString)"
}
一些解决方案

告诉我将文件转换为数据,然后将其发送到服务器,而一些解决方案则说直接将文件路径添加到您的身体中。需要帮助!

一个根本错误是您在URLSession实例上使用dataTask而不是上传任务,例如 uploadTask(with:from:completionHandler:)

身体数据的构建

以下是我自己的代码(如下面的注释中要求(中关于如何构建正文数据的通用示例:

// imagesURLS is an optional array of URLs, i.e. imageURLS:[URL]?
if let imgURLs = imagesURLS {
    for f in imgURLs {
        let filename = f.lastPathComponent  
        let splitName = filename.split(separator: ".")
        let name = String(describing: splitName.first)
        let filetype = String(describing: splitName.last)
        let imgBoundary = "rn--(boundary)rnContent-Type: image/(filetype)rnContent-Disposition: form-data; filename=(filename); name=(name)rnrn"
        if let d = imgBoundary.data(using: .utf8) {
            bodyData.append(d)
        }
        do {
            let imgData = try Data(contentsOf:f, options:[])
            bodyData.append(imgData)
        }
        catch {
            // can't load image data
        }
        }
    }
    let closingBoundary = "rn--(boundary)--"
    if let d = closingBoundary.data(using: .utf8) {
            bodyData.append(d)
    }

循环意味着每个数据项(在本例中为图像(前面都有一个边界字符串,在最后一项数据之后添加结束边界字符串(即以双连字符结尾的边界字符串(。

这在 Swift4 中对我有用:

func uploadFiles(_ urlPath: [URL]){
if let url = URL(string: "YourURL"){
var request = URLRequest(url: url)
let boundary:String = "Boundary-(UUID().uuidString)"
request.httpMethod = "POST"
request.timeoutInterval = 10
request.allHTTPHeaderFields = ["Content-Type": "multipart/form-data; boundary=----(boundary)"]
    for path in urlPath{
        do{
            var data2: Data = Data()
            var data: Data = Data()
            data2 = try NSData.init(contentsOf: URL.init(fileURLWithPath: path.absoluteString, isDirectory: true)) as Data
            /* Use this if you have to send a JSON too.
             let dic:[String:Any] = [
             "Key":Value,
             "Key":Value
            ]

            for (key,value) in dic{
                data.append("------(boundary)rn")
                data.append("Content-Disposition: form-data; name="(key)"rnrn")
                data.append("(value)rn")
            }
              */
            data.append("------(boundary)rn")
            //Here you have to change the Content-Type
            data.append("Content-Disposition: form-data; name="file"; filename="YourFileName"rn")
            data.append("Content-Type: application/YourTypernrn")
            data.append(data2)
            data.append("rn")
            data.append("------(boundary)--")
            request.httpBody = data
        }catch let e{
            //Your errors
        }
        DispatchQueue.global(qos: DispatchQoS.QoSClass.userInitiated).sync {
            let session = URLSession.shared
            let task = session.dataTask(with: request, completionHandler: { (dataS, aResponse, error) in
                if let erros = error{
                    //Your errors
                }else{
                    do{
                        let responseObj = try JSONSerialization.jsonObject(with: dataS!, options: JSONSerialization.ReadingOptions(rawValue:0)) as! [String:Any]
                    }catch let e{
                    }
                }
            }).resume()
        }
    }
}
}
extension Data{
mutating func append(_ string: String, using encoding: String.Encoding = .utf8) {
    if let data = string.data(using: encoding) {
        append(data)
    }
}
}
<</div> div class="one_answers">上传

图片的示例代码为:

func uploadImage(){
    var imageToUpload:UIImage = UIImage()
    let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
    let nsUserDomainMask    = FileManager.SearchPathDomainMask.userDomainMask
    let paths               = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
    if let dirPath          = paths.first
    {
        let imageURL = URL(fileURLWithPath: dirPath).appendingPathComponent("Image2.png") //Your image name here
        let image    = UIImage(contentsOfFile: imageURL.path)
        imageToUpload = image!
    }

    Alamofire.upload(multipartFormData: { (multipartFormData) in
        multipartFormData.append(UIImageJPEGRepresentation(imageToUpload, 1)!, withName: "Prescription", fileName: "Profile_Image.jpeg", mimeType: "image/jpeg")
    }, to:"you_URL_here")
    { (result) in
        switch result {
        case .success(let upload, _, _):
            print(result)
            upload.uploadProgress(closure: { (progress) in
                print(progress)
            })
            upload.responseJSON { response in
                //print response.result
                print(response);
            }
        case .failure(let encodingError):
            print(encodingError);
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新