首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用AFNetworking从AVCapture上传文件

使用AFNetworking从AVCapture上传文件
EN

Stack Overflow用户
提问于 2015-04-23 20:22:53
回答 2查看 622关注 0票数 6

我有一个用AVCapture捕获的视频,我正在尝试用AFNetworking和Swift上传。

代码:

代码语言:javascript
复制
let manager = AFHTTPRequestOperationManager()
let url = "http://localhost/test/upload.php"
var fileURL = NSURL.fileURLWithPath(string: ViewControllerVideoPath)
var params = [
    "familyId":locationd,
    "contentBody" : "Some body content for the test application",
    "name" : "the name/title",
    "typeOfContent":"photo"
]

manager.POST( url, parameters: params,
    constructingBodyWithBlock: { (data: AFMultipartFormData!) in
        println("")
        var res = data.appendPartWithFileURL(fileURL, name: "fileToUpload", error: nil)
        println("was file added properly to the body? \(res)")
    },
    success: { (operation: AFHTTPRequestOperation!, responseObject: AnyObject!) in
        println("Yes thies was a success")
    },
    failure: { (operation: AFHTTPRequestOperation!, error: NSError!) in
        println("We got an error here.. \(error.localizedDescription)")
})

上面的代码失败了,我一直收到

代码语言:javascript
复制
was file added properly to the body? false"

请注意,ViewControllerVideoPath是一个包含视频位置的字符串,它是:

代码语言:javascript
复制
"/private/var/mobile/Containers/Data/Application/1110EE7A-7572-4092-8045-6EEE1B62949/tmp/movie.mov" 

使用println()...当我上传目录中包含的文件并使用以下命令时,上面的代码可以正常工作:

代码语言:javascript
复制
 var fileURL = NSURL.fileURLWithPath(NSBundle.mainBundle().pathForResource("test_1", ofType: "mov")!)

因此,我的PHP代码肯定是好的,问题在于上传保存在设备上的文件,我在这里做错了什么?

EN

回答 2

Stack Overflow用户

发布于 2015-04-28 05:15:14

评论不允许完整的解释,所以这里有更多的信息;

NSBundle.mainBundle()引用捆绑包文件中的路径,模拟器中的路径与应用程序中的路径不同...这不是你想要的。根据你的需要,你可以访问许多“文件夹”(私有的或可共享的/文件,它们可以备份到云中)。NSPathUtils.h提供了可用路径的细目。为了与大多数人使用的约定保持一致,您可能应该通过执行以下操作在应用程序路径下创建一个私有路径;

代码语言:javascript
复制
  - (NSURL *) applicationPrivateDocumentsDirectory{
  NSURL *pathURL = [[self applicationLibraryDirecory]URLByAppendingPathComponent:@"MyApplicationName"];
  return pathURL;

}

代码语言:javascript
复制
  - (NSURL *) applicationLibraryDirecory{

  return [[[NSFileManager defaultManager] URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask] lastObject];

}

您可以测试它是否存在,如果不存在,则创建它...然后将视频文件存储在此路径中,并将其作为存储文件的位置传递给AVCapture。

票数 2
EN

Stack Overflow用户

发布于 2015-05-01 14:29:48

下面是可以在swift中实现以下功能的代码。

1 :检查天气目录是否存在。如果不存在,则在文档目录文件夹中创建目录(目录已指定应用程序名称)。

2 :现在我们有了应用程序目录。因此应用程序中所有文件都将在此目录中写入/读取。

代码语言:javascript
复制
   let file = "file.txt"
    let directoryName = “XYZ” // Here “XYZ” is project name.
    var error : NSError?
    let filemgr = NSFileManager.defaultManager()

    let dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, 
                .UserDomainMask, true)

    let documentsDirectory = dirPaths[0] as! String 

    var dataPath = documentsDirectory.stringByAppendingPathComponent(directoryName)

    if !NSFileManager.defaultManager().fileExistsAtPath(dataPath) {
        NSFileManager.defaultManager().createDirectoryAtPath(dataPath, withIntermediateDirectories: false, attributes: nil, error: &error)
    } else {
        println("not creted or exist")
    }

现在我们有了目录,所以只需要从目录中写入/读取数据。

如何在swift中将文件写入文档目录

代码语言:javascript
复制
let filePath = dataPath.stringByAppendingPathComponent(file);
    let text = "some text"

//writing
text.writeToFile(filePath, atomically: false, encoding: NSUTF8StringEncoding, error: nil);

如何从文档目录中读取文件。

代码语言:javascript
复制
let filePath = dataPath.stringByAppendingPathComponent(file);
// Read file
let text2 = String(contentsOfFile: filePath, encoding: NSUTF8StringEncoding, error: nil)

输出:

希望这能对你有所帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29823373

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档