ios - 在 iOS 上使用 Django 和 Alamofire 上传图像

标签 ios swift django alamofire

我正在尝试将图像从我的 iOS 客户端应用程序上传到我的 Django 后端服务器。这是我在客户端所做的事情:

func submitPhotoAndInfo(photo: UIImage, info: Info, completionHandler: @escaping (Bool, String?) -> Void) {
    let headers = ["Authorization": "Token " + self.accessToken!, "Content-type": "multipart/form-data"]
    self.showNetworkActivity(active: true)
    Alamofire.upload(multipartFormData: { multipartFormData in
        do {
            let dict = try info.asDictionary()
            for (key, value) in dict {
                if value is String || value is Int {
                    multipartFormData.append("\(value)".data(using: .utf8)!, withName: key)
                }
            }
        } catch let error as NSError {
            completionHandler(false, error.localizedDescription)
        }
        let imageData = UIImageJPEGRepresentation(photo, 0.8)
        multipartFormData.append(imageData!, withName: “my_photo", fileName: “my_photo.jpg", mimeType: "jpg/png")
    }, usingThreshold: UInt64.init(), to: "https://myapp.herokuapp.com/users/submit_photo_and_info", method: .post, headers: headers) { encodingResult in
        self.showNetworkActivity(active: false)
        switch encodingResult {
        case .success(let response, _, _):
            if response.response?.statusCode == 200 {
                completionHandler(true, nil)
            } else {
                completionHandler(false, "Server error")
            }
        case .failure(let error):
            print(error)
            completionHandler(false, error.localizedDescription)
        }
    }
}

这是在 Django 服务器上:

@api_view(['POST'])
@parser_classes((MultiPartParser,))
@authentication_classes((TokenAuthentication,))
def submit_photo_and_info(request):
    file_obj = request.data[‘photo']
    reg = Info()
    reg.user = request.user
    reg.photo = request.data[‘photo']
    reg.status = 'aw'
    try:
        resp = requests.get(“photo")
        resp.raise_for_status()
    except Exception as e:
        logging.error(e)
        return Response({}, status=status.HTTP_406_NOT_ACCEPTABLE)
    image_file = ContentFile(resp.content)
    reg.photo.save(str(user.id) + ".jpg", image_file)
reg.save()
return Response({}, status=status.HTTP_200_OK)

似乎有一些基本的错误,因为我在服务器上收到这些错误:

Aug 28 13:58:28 myapp app/web.1: 10.35.220.176 - - [28/Aug/2018:13:58:27 -0700] "POST /users/submit_photo_and_info HTTP/1.1" 301 0 "-“ "MyApp/0.1 (com.myapp.MyApp; build:25; iOS 11.4.1) Alamofire/4.7.2" 
Aug 28 13:58:28 myapp heroku/router: sock=backend at=error code=H18 desc="Server Request Interrupted" method=POST path="/users/submit_photo_and_info" host=myapp.herokuapp.com request_id=358902be-53e8-4a49-a96e-34d6a45a2b95 fwd="23.242.1.246" dyno=web.1 connect=1ms service=510ms status=503 bytes=220 protocol=https 

有什么想法吗?

最佳答案

请尝试我的方法:

NetworkManager.shared.sessionManager = Alamofire。 不要忘记设置您的 URL。

    // Upload file
static func uploadImage(image: UIImage, fileType: String, completion: @escaping (_ success: Bool,_ imageId: Int, _ error: Error?) -> Void) {

     let url  = Api.baseUrl + Api.OperationService.UploadImage.URL + fileType

    NetworkManager.shared.sessionManager.upload(multipartFormData: { multipartFormData in
        if let imageData = UIImageJPEGRepresentation(image, 0.5) {
            multipartFormData.append(imageData, withName: "file", fileName: "image", mimeType: "image/jpeg")
        }}, to: url, method: .post,
            encodingCompletion: { encodingResult in
                switch encodingResult {
                case .success(let upload, _, _):
                    upload.responseJSON { response in
                        debugPrint(response)
                        if let value = response.value as? [String: AnyObject] {
                            guard let imageId = value["id"] as? Int else {
                                return
                            }
                            completion(true, imageId, nil)
                        } else {
                            completion(false, 0, nil)
                        }
                    }
                case .failure(let error):
                    completion(false, 0, error.localizedDescription as? Error)
                }
    })
}

关于ios - 在 iOS 上使用 Django 和 Alamofire 上传图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52066635/

相关文章:

iOS如何将图像的一部分绘制到特定的矩形?

ios - 快速上传图像 AWS S3 存储桶

javascript - WebGL GLSL 片段着色器不适用于 iOS

ios - 在代码中将最小值和最大值分配给 WKInterfaceSlider

python - 如何在 Django View 中创建模型对象?

python - 标记库的 Django 注册不起作用

ios - GPUImage swift iOS 7

java - 是否有低级 Swift 类可以在 AWS S3 中上传文件

unit-testing - 单元测试用例 View Controller 崩溃 swift

python - list_display 不起作用