Alamofire上传巨大的文件 [英] Alamofire upload huge file

查看:439
本文介绍了Alamofire上传巨大的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Alamofire上载资产(图像/视频)作为多部分表单数据.对于小于300MB(应用程序)的文件大小,它可以正常工作.当我尝试上传大于300MB的文件时,应用崩溃.

I am using Alamofire to upload assets (image/video) as multipart form data. It works fine for file sizes below 300MB (app). When I try to upload a file greater than 300MB, app crashes.

if let video = self.avPlayerItem?.asset as? AVURLAsset {
    if let assetData = NSData(contentsOfURL: video.URL) {
        multipartFormData.appendBodyPart(data: assetData, name: "file", fileName: "video", mimeType: "video/mp4") // Execution stops here
    }
}

我也从Xcode收到以下消息

I also get the below message from Xcode

我将如何支持使用Alamofire上传大型视频?

How would I support uploading huge sized videos using Alamofire?

推荐答案

使用Stream上传而不是将文件转换为NSData,这会导致内存问题并在上传大文件时发生崩溃. 示例代码

Use Stream to upload instead of converting file to NSData which lead to memory problem and occur crash while uploading huge files. sample code

  if let imageUrl = info[UIImagePickerControllerReferenceURL] as? NSURL{
        let assetLibrary = ALAssetsLibrary()
        assetLibrary.assetForURL(imageUrl , resultBlock: { (asset: ALAsset!) -> Void in
            if let actualAsset = asset as ALAsset? {
                let assetRep: ALAssetRepresentation = actualAsset.defaultRepresentation()

                let size = assetRep.size()
                let stream = NSInputStream(URL: assetRep.url())

                Alamofire.upload(
                    .POST,
                    "SERVER_URL",
                    headers: [:],
                    multipartFormData: { multipartFormData in

                         multipartFormData.appendBodyPart(stream: stream!, length: UInt64(size), name: "fileparameter", fileName: "fileName", mimeType: "video/mp4")

                    },
                    encodingCompletion: { encodingResult in
                        switch encodingResult {
                        case .Success(let upload, _, _):
                            upload.progress { bytesWritten, totalBytesWritten, totalBytesExpectedToWrite in
                                dispatch_async(dispatch_get_main_queue()) {
                                    let percent = (Float(totalBytesWritten) / Float(totalBytesExpectedToWrite))
                                    print(percent)
                                }
                            }
                            upload.validate()
                            upload.responseJSON { response in
                                print(response);
                            }
                        case .Failure(let encodingError):
                            print(encodingError)
                            let error = NSError(domain: "", code: 404, userInfo: [NSLocalizedDescriptionKey: "Image Uploading Failed. Please try again."])
                            let result = Result<AnyObject, NSError>.Failure(error)
                            let response = Response(request: nil, response: nil, data: nil, result: result)
                             print(response);

                        }
                    }
                )

            }
            }, failureBlock: { (error) -> Void in
        })
    }

这篇关于Alamofire上传巨大的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆