簡體   English   中英

將圖像上傳到服務器Swift 3

[英]Upload image to server Swift 3

我正在嘗試使用Swift 3將圖像上傳到iOS中的服務器,我已經嘗試使用Alamofire了,但是它不起作用,所以我只是在此論壇中搜索了另一個解決方案,但是沒有運氣。

我找到了一些答案,說問題可能出在服務器端,但是在Android上,圖像可以正確上傳。

這是我快速上傳的功能3:

func uploadImage(image: UIImage){
         let imageData = UIImageJPEGRepresentation(image, 0.1)!

         let session = URLSession(configuration: URLSessionConfiguration.default)

         guard let url = URL(string: uploadPicUrl) /* your API url */) else { return }
         var request = URLRequest(url: url)

         request.httpMethod = "POST"

         let boundary = "---------------------------14737809831466499882746641449"
         let contentType = "multipart/form-data; boundary=\(boundary)"
         request.addValue(contentType, forHTTPHeaderField: "Content-Type")

         var body = Data()
         body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
         body.append("Content-Disposition: form-data; name=\"userfile\"; filename=\"img.jpg\"\r\n".data(using: String.Encoding.utf8)!)
         body.append("Content-Transfer-Encoding: binary\r\n\r\n".data(using: String.Encoding.utf8)!)
         body.append(imageData)
         body.append("\r\n".data(using: String.Encoding.utf8)!)
         body.append("--\(boundary)--\r\n".data(using: String.Encoding.utf8)!)

         request.httpBody = body

         print("request", request.debugDescription)
         print("body", body.debugDescription)

         let dataTask = session.dataTask(with: request) { (data, response, error) in

         if let error = error {
         print("Something went wrong: \(error)")
         }

         if let response = response {
         print("Response: \n \(response)")
         }
         }

         dataTask.resume()
}

不使用Alamofire,您可以執行以下操作:

func uploadImage(chosenimage: UIImage) {

    let url = ApiList.base_url + ApiList.uploadFile_Url
    let myUrl = NSURL(string: url)
    let image_data = UIImagePNGRepresentation(chosenimage)
    let tempData = NSMutableData()
    let request = NSMutableURLRequest(url:myUrl! as URL)
    request.httpMethod = "POST"
    let boundary = NSUUID().uuidString
    request.addValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField:"Content-Type")
    let mimetype = "image/png"
    let fname = "test.png"
    self.body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
    self.body.append("Content-Disposition:form-data; name=\"profileUrl\"; filename=\"\(fname)\"\r\n".data(using: String.Encoding.utf8)!)
    self.body.append("Content-Type: \(mimetype)\r\n\r\n".data(using: String.Encoding.utf8)!)
    self.body.append(image_data!)
    self.body.append("\r\n".data(using: String.Encoding.utf8)!)

    let accessToken = UserDefaults.standard.value(forKey: "accessToken") as? String ?? ""
    let deviceToken = UserDefaults.standard.value(forKey: "deviceToken") as? String ?? singletonclass.instance.getDeviceToken

    let param = [

        "accessToken":accessToken,
        "deviceId":deviceToken,
        "deviceType":"2"
    ]

    for (key,value) in param {

        tempData.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
        tempData.append("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n".data(using: String.Encoding.utf8)!)
        tempData.append("\(value)\r\n".data(using: String.Encoding.utf8)!)

    }
    self.body.append(tempData as Data)
    self.body.append("--\(boundary)--\r\n".data(using: String.Encoding.utf8)!)
    request.httpBody = self.body as Data

    let session = URLSession.shared

    let task = session.dataTask(with: request as URLRequest) {
        (
        data, response, error) in

        guard let _:NSData = data! as NSData, let _:URLResponse = response, error == nil else { return }
        do
        {
            let responseDict = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.allowFragments) as! [String:Any]
            //print("\n tyiukmqw",responseDict)

            let code = responseDict.value(forKey: "code") as? String
            let message = responseDict.value(forKey: "Message") as? String
            singletonclass.instance.isrofilePicChagedOrNot = false

            if code == "200"
            {
                print("success code")

                DispatchQueue.main.async(execute: {

                    self.userProfile.image = chosenimage
                    UserDefaults.standard.setValue(UIImagePNGRepresentation(chosenimage), forKey: "UserProfilePicture")
                    singletonclass.instance.userProPic = chosenimage

                })
            }
            else
            {
                DispatchQueue.main.async(execute: {

                    singletonclass.instance.showAlert(message!)
                    self.isActivityIndicatorNeed(false)

                })
            }
        }
        catch
        {

        }
    }

    task.resume()
}

注意:此功能是使用Alamofire上傳不同類型的多個文件

// upload file to server
func uploadFiles(files: [Data],completion : @escaping uploadHandler) {

let header : HTTPHeaders = ["Content-Type" : "application/x-www-form-urlencoded"] // if there's Authorization, you may add it in header

let url = URL(string: "Enter the url here")

    Alamofire.upload(multipartFormData: { (multipartFormData) in
        for document in files {
            let fileName = NSUUID().uuidString
            multipartFormData.append(document, withName: "documents", fileName:fileName ,mimeType: "image/jpeg") // You'll have to define the proper mime time for uploading other type of files. You may achieve it by creating a struct and storing the type of each file.
        }
    }, usingThreshold: UInt64.init(), to:url, method: .post, headers: header) { (result) in
        switch result{
        case .success(let upload, _, _):
            upload.responseJSON { response in
                switch response.result {
                case .success(_) : completion(true, nil)
                case .failure(let error) :
                    print("Error in upload: \(error.localizedDescription)")
                    completion(false, nil)
                }
            }
        case .failure(let error):
            print("Error in upload: \(error.localizedDescription)")
            completion(false,error)
        }
    }

}

typealias uploadHandler = (_ status :Bool,_ error :Error?)->()// Define this anywhere globally 

在調用該函數時,我傳遞了一個文件數組,我將不同類型的文件轉換為數據並將其上傳到服務器。

如果要比調用函數之前上傳圖像數組。

var documents = [Data]()
for image in imageArray {
       if let imgData  = UIImageJPEGRepresentation(image, 1.0) {
        documents.append(document)

         }     
       }

現在,您可以調用上載函數並傳遞文檔並收聽完成處理程序。

祝好運 。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM