如何使用 Swift 和 Alamofire 成功将视频文件上传到 imgur?

Posted

技术标签:

【中文标题】如何使用 Swift 和 Alamofire 成功将视频文件上传到 imgur?【英文标题】:How to successfully upload video file to imgur using Swift and Alamofire? 【发布时间】:2020-05-31 05:42:33 【问题描述】:

这是我来自 imgur 的回复:


  "status" : 200,
  "data" : 
    "errorCode" : null,
    "ticket" : "c4fb887c"
  ,
  "success" : true

如何创建请求?

let video = //here I have URL of video, for example: file:///private/var/mobile/Containers/Data/PluginKitPlugin/FAB4AFC7-56C6-4949-8938-31DCD65A13CB/tmp/trim.B31D7604-B991-4D89-AD61-18DE9D9FD7FD.MOV
let url = URL(string: "https://api.imgur.com/3/image")!
Alamofire.upload(multipartFormData:  multipart in
    if let video = image.video 
        let data = try! Data(contentsOf: video, options: .mappedIfSafe)
        multipart.append(data, withName: "video", fileName: "video.mp4", mimeType: "video/mp4")
    
, to: url, headers: headers, encodingCompletion:  result in
    if case .success(let upload, _, _) = result 
        upload.responseJSON  response in
            let json = JSON(response.data)
            print(json)
        
    
)

我会期待什么?

一些视频链接

【问题讨论】:

【参考方案1】:

您需要做的就是将您的网址替换为:

let url = URL(string: "https://api.imgur.com/3/upload")!

然后你会得到这样的回应:


  "data" : 
    "ad_url" : null,
    "mp4" : "https:\/\/i.imgur.com\/GD86Stz.mp4",
    "datetime" : 1590905085,
    "in_gallery" : false,
    "account_id" : null,
    "title" : null,
    "width" : 0,
    "height" : 0,
    "nsfw" : null,
    "bandwidth" : 0,
    "name" : "",
    "is_ad" : false,
    "size" : 0,
    "description" : null,
    "vote" : null,
    "views" : 0,
    "has_sound" : false,
    "ad_type" : null,
    "account_url" : null,
    "in_most_viral" : false,
    "link" : "https:\/\/i.imgur.com\/GD86Stz.mp4",
    "processing" : 
      "status" : "pending"
    ,
    "deletehash" : "twCk1gbdjcp4FmH",
    "favorite" : false,
    "id" : "GD86Stz",
    "section" : null,
    "animated" : true,
    "type" : "video\/mp4",
    "tags" : [

    ],
    "hls" : ""
  ,
  "status" : 200,
  "success" : true

【讨论】:

以上是关于如何使用 Swift 和 Alamofire 成功将视频文件上传到 imgur?的主要内容,如果未能解决你的问题,请参考以下文章

如何将验证错误从 php 返回到 swift 或 segue 成功

Swift - 使用 alamofire 发布请求

如何使用 Alamofire 和 Swift Decode 从 JSON 解码和访问 Double

在 Swift 中将数组转换为 Alamofire 参数

Alamofire API 连接和 Swift

如何在swift中使用alamofire和swiftyjson解析分页url?