文件上载从迅疾的Django [英] File upload from swift to django

查看:205
本文介绍了文件上载从迅疾的Django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从快速的iOS应用上传照片和视频到Amazon S3使用Django的后端。我发现了一个Django应用程序连接的Django使用到S3 https://github.com/bradleyg/django-s3direct 问题是我不知道如何从飞跑Django的上传文件。

I am trying to upload photos and videos from swift iOS app to amazon s3 using django backend. I found a django app to connect django to s3 using https://github.com/bradleyg/django-s3direct Problem is I have no idea how to upload a file from swift to django.

Django的-s3direct我得到了工作在管理面板,他们提供了一个很好的方法来生成表单来上传文件,但我不知道如何使用这个插件Django的迅速上传的文件。

The django-s3direct I got working in the admin panel and they provide a nice method to generate a form for uploading the files but I don't know how to upload the files from swift using this plugin for django.

我应该找到Django的后端连接到Amazon S3不同的方式?是否有一个教程就在某个地方的呢? (我找不到任何)

Should I find a different way of connecting the django backend to amazon s3? Is there a tutorial out there somewhere for this? (I can't find any)

推荐答案

您认为应该能够收到文件 POST 要求和经常的事情,当你收到上传的图片从常规的HTML表单做。那么所有你所要做的就是在iOS中创建的多部分的形式,并将其发布到接收器的URL。一旦你提交照片的形式,你的Django应用程序应该做的S3的事情。所以,简单地你的问题是如何从发布的iOS所选图像到Web服务的形式为多部分的形式。这code证明。

Your view should be able to receive FILES with the POST request and the regular thing to do when you receive an uploaded picture from regular html form. then all you have to do is to create multipart form in iOS and post it to the receiver url. Once you submit the photo in the form, you django application should do the S3 thing. So simply your question is how to post a selected image from iOS to a web-service form as a multipart form. This code demonstrate that.

var imageData :NSData = UIImageJPEGRepresentation(imagenReduced, 1.0);       
var request: NSMutableURLRequest?
let HTTPMethod: String = "POST"
var timeoutInterval: NSTimeInterval = 60
var HTTPShouldHandleCookies: Bool = false

request = NSMutableURLRequest(URL: url)
request!.HTTPMethod = HTTPMethod
request!.timeoutInterval = timeoutInterval
request!.HTTPShouldHandleCookies = HTTPShouldHandleCookies


let boundary = "----------SwIfTeRhTtPrEqUeStBoUnDaRy"
let contentType = "multipart/form-data; boundary=\(boundary)"
request!.setValue(contentType, forHTTPHeaderField:"Content-Type")
var body = NSMutableData();


 let tempData = NSMutableData()
 let fileName = filenames + ".jpg" //"prueba.jpg"
 let parameterName = "userfile"


let mimeType = "application/octet-stream"

tempData.appendData("--\(boundary)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
let fileNameContentDisposition = fileName != nil ? "filename=\"\(fileName)\"" : ""
let contentDisposition = "Content-Disposition: form-data; name=\"\(parameterName)\"; \(fileNameContentDisposition)\r\n"
tempData.appendData(contentDisposition.dataUsingEncoding(NSUTF8StringEncoding)!)
tempData.appendData("Content-Type: \(mimeType)\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
tempData.appendData(imageData)
tempData.appendData("\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)

body.appendData(tempData)

body.appendData("\r\n--\(boundary)--\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)

request!.setValue("\(body.length)", forHTTPHeaderField: "Content-Length")
request!.HTTPBody = body



var vl_error :NSErrorPointer = nil
var responseData  = NSURLConnection.sendSynchronousRequest(request,returningResponse: nil, error:vl_error)

var results = NSString(data:responseData, encoding:NSUTF8StringEncoding)
println("finish \(results)")

这篇关于文件上载从迅疾的Django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆