swift 上传图片和参数 upload image with params

swift 上传图片和参数 upload image with params,第1张

概述let docDir:AnyObject = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]let imagePath = docDir + "/myPic.jpg"var imageData = NSData(contentsOfFile: imagePath, option

let docDir:AnyObject = NSSearchPathForDirectorIEsInDomains(.documentDirectory,.UserDomainMask,true)[0]let imagePath = docDir + "/myPic.jpg"var imageData = NSData(contentsOffile: imagePath,options: NSDataReadingOptions.DataReadingMappedIfSafe,error: nil)var parameters = [ "pic" :NetData(nsData: imageData!,filename: "customname.jpg"),"otherParm" :"Value" ] let urlRequest = self.urlRequestWithComponents("http://www.example.com/upload.PHP",parameters: parameters)

Alamofire.upload(urlRequest.0,urlRequest.1) .progress { (bytesWritten,totalBytesWritten,totalBytesExpectedToWrite) in println("\(totalBytesWritten) / \(totalBytesExpectedToWrite)") } }
 
func urlRequestWithComponents(urlString:String,parameters:NSDictionary) -> (URLRequestConvertible,NSData) {    // create url request to send    var mutableURLRequest = NSMutableURLRequest(URL: NSURL(string: urlString)!)    mutableURLRequest.httpMethod = Alamofire.Method.POST.rawValue    //let boundaryConstant = "myRandomBoundary12345"    let boundaryConstant = "NET-POST-boundary-\(arc4random())-\(arc4random())"    let ContentType = "multipart/form-data;boundary="+boundaryConstant    mutableURLRequest.setValue(ContentType,forhttpheaderFIEld: "Content-Type")    // create upload data to send    let uploadData = NSMutableData()    // add parameters    for (key,value) in parameters {        uploadData.appendData("\r\n--\(boundaryConstant)\r\n".dataUsingEnCoding(NSUTF8StringEnCoding)!)        if value is NetData {            // add image            var postData = value as NetData            //uploadData.appendData("Content-disposition: form-data; name=\"\(key)\"; filename=\"\(postData.filename)\"\r\n".dataUsingEnCoding(NSUTF8StringEnCoding)!)            // append content disposition            var filenameClause = " filename=\"\(postData.filename)\""            let contentdispositionString = "Content-disposition: form-data; name=\"\(key)\";\(filenameClause)\r\n"            let contentdispositionData = contentdispositionString.dataUsingEnCoding(NSUTF8StringEnCoding)            uploadData.appendData(contentdispositionData!)            // append content type            //uploadData.appendData("Content-Type: image/png\r\n\r\n".dataUsingEnCoding(NSUTF8StringEnCoding)!) // mark this.             let ContentTypeString = "Content-Type: \(postData.mimeType.getString())\r\n\r\n"            let ContentTypeData = ContentTypeString.dataUsingEnCoding(NSUTF8StringEnCoding)            uploadData.appendData(ContentTypeData!)            uploadData.appendData(postData.data)        }else{            uploadData.appendData("Content-disposition: form-data; name=\"\(key)\"\r\n\r\n\(value)".dataUsingEnCoding(NSUTF8StringEnCoding)!)        }    }    uploadData.appendData("\r\n--\(boundaryConstant)--\r\n".dataUsingEnCoding(NSUTF8StringEnCoding)!)    // return URLRequestConvertible and NSData    return (Alamofire.ParameterEnCoding.URL.encode(mutableURLRequest,parameters: nil).0,uploadData)}
总结

以上是内存溢出为你收集整理的swift 上传图片参数 upload image with params全部内容,希望文章能够帮你解决swift 上传图片和参数 upload image with params所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1077754.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存