6

AVCapture でキャプチャしたビデオがあり、Swift を使用して AFNetworking でアップロードしようとしています。

コード:

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)")
})

上記のコードは失敗し、取得し続けます

was file added properly to the body? false"

ViewControllerVideoPath次のビデオの場所を含む文字列であることに注意してください。

"/private/var/mobile/Containers/Data/Application/1110EE7A-7572-4092-8045-6EEE1B62949/tmp/movie.mov" 

using println()....上記のコードは、ディレクトリに含まれるファイルをアップロードし、次を使用すると機能します。

 var fileURL = NSURL.fileURLWithPath(NSBundle.mainBundle().pathForResource("test_1", ofType: "mov")!)

したがって、間違いなく私のPHPコードは問題ありません。問題は、デバイスに保存されたファイルのアップロードにあります。ここで何が間違っていますか?

4

2 に答える 2

1

これは、次の機能を迅速に実行できるコードです。

1 :天気ディレクトリが存在するかどうかを確認します。存在しない場合は、ドキュメント ディレクトリ フォルダーにディレクトリ (ディレクトリにはアプリケーション名が指定されています) を作成します。

2 :これで、アプリケーション ディレクトリが作成されました。したがって、アプリケーションからこのディレクトリに書き込み/読み取りを行うすべてのファイル。

   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でドキュメントディレクトリにファイルを書き込む方法

let filePath = dataPath.stringByAppendingPathComponent(file);
    let text = "some text"

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

ドキュメント ディレクトリからファイルを読み取る方法。

let filePath = dataPath.stringByAppendingPathComponent(file);
// Read file
let text2 = String(contentsOfFile: filePath, encoding: NSUTF8StringEncoding, error: nil)

出力: ここに画像の説明を入力

これがあなたを助けることを願っています。

于 2015-05-01T06:29:48.273 に答える