2016-12-19 25 views
2

我试图从IOS Swift 3上传一个Sqlite数据库到我的服务器,使用Alamofire 4.0,但是在将sqlite文件转换为上传所需的数据类型时出现问题。Alamofire v4,Swift v3将Sqlite文件上传到域

大部分职位/问题的例子似乎默认上传图片,但我在努力寻找例如上传sqlite的或其他类型的文件(用于备份目的)

的...我已经寻找的基本代码,发现这个至今看起来很合理(感谢下面的帖子:Alamofire 4 upload with parameters

let parameters = ["file_name": "swift_file.jpeg"] 

Alamofire.upload(multipartFormData: { (multipartFormData) in 
    multipartFormData.append(UIImageJPEGRepresentation(self.photoImageView.image!, 1)!, withName: "photo_path", fileName: "swift_file.jpeg", mimeType: "image/jpeg") 
    for (key, value) in parameters { 
     multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key) 
    } 
}, to:"http://sample.com/upload_img.php") 
{ (result) in 
    switch result 
    { 
    case .success(let upload, _, _): 
    upload.uploadProgress(closure: { (progress) in 
     //Print progress 
    }) 
    upload.responseJSON { response in 
     //print response.result 
    } 
    case .failure(let encodingError): 
    //print encodingError.description 
    } 
} 

我与挣扎的部分是到sqlite的文件附加到上传(multipartFormData.append(......... .. ?)我搜索了但没有找到任何好的参考文章

是的,我是一个newbe,但是努力,任何帮助,将不胜感激......

回答

2

这是完全一样的,不同的是MIME类型将是application/octet-stream情况的图像例。

此外,您可能会直接从fileURL加载它,而不是首先将其加载到Data

另外,该示例中的parameters不太合理,因为它看起来与图像本身上传中提供的文件名是多余的。所以你可以使用你的Web服务需要的任何参数,如果有的话。如果没有其他参数,则完全省略完整的for (key, value) { ... }循环。

最后,显然用您的Web服务正在查找的任何字段名称替换file字段名称。

// any additional parameters that must be included in the request (if any) 

let parameters = ["somekey": "somevalue"] 

// database to be uploaded; I'm assuming it's in Documents, but perhaps you have it elsewhere, so build the URL appropriately for where the file is 

let filename = "test.sqlite" 
let fileURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false) 
    .appendingPathComponent(filename) 

// now initiate request 

Alamofire.upload(multipartFormData: { multipartFormData in 
    multipartFormData.append(fileURL, withName: "file", fileName: filename, mimeType: "application/octet-stream") 

    for (key, value) in parameters { 
     multipartFormData.append(value.data(using: .utf8)!, withName: key) 
    } 
}, to: urlString) { result in 
    switch result { 
    case .success(let upload, _, _): 
     upload 
      .authenticate(user: self.user, password: self.password) // only needed if you're doing server authentication 
      .uploadProgress { progress in 
       print(progress.fractionCompleted) 
      } 
      .responseJSON { response in 
       print("\(response.result.value)") 
     } 
    case .failure(let encodingError): 
     print(encodingError.localizedDescription) 
    } 
} 

无关,但如果您不确定为使用什么MIME类型,你可以使用这个程序,该程序将尝试确定从文件扩展名MIME类型。

/// Determine mime type on the basis of extension of a file. 
/// 
/// This requires MobileCoreServices framework. 
/// 
/// - parameter url: The file `URL` of the local file for which we are going to determine the mime type. 
/// 
/// - returns:  Returns the mime type if successful. Returns application/octet-stream if unable to determine mime type. 

func mimeType(for url: URL) -> String { 
    let pathExtension = url.pathExtension 

    if let uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension as NSString, nil)?.takeRetainedValue() { 
     if let mimetype = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType)?.takeRetainedValue() { 
      return mimetype as String 
     } 
    } 
    return "application/octet-stream"; 
} 
+0

感谢您的帮助,我认为一切正常,但我急于读取服务器上的错误目录。该文件没有被上传。在苹果的代码都很好看,但是 – maper1

+0

这是用于创建多部分请求的合适的Alamofire代码。当我这样做时,我看到了进展,它出现在我的服务器上。我怀疑是服务器代码和客户端代码断开连接。没有看到服务器代码,我不确定我能否进一步提供建议。 – Rob

+0

它确实看起来像服务器的概率,但文件与android一起工作,也与swiftv2(正在使用SRwebclient) 完成的“progress.fraction”打印几次0.01322值....然后报告“无” : 我加入upload.response: .request .response 。数据 。结果 .timeline给予: 可选( {的URL:http://mydomain/fileup.aspx} {状态代码:500,头文件“Cache-Control”= private; Connection = close; “Content-Type”=“text/html”;“X-AspNet-Version”=“4.0.30319”; 可选(6411字节) 失败 - 任何帮助? – maper1