使用 Alamofire 上传带参数的文件
我正在尝试使用 Alamofire
上传文件.使用文件 (NSUrl
) 时上传工作正常,但是,我似乎无法弄清楚如何使用 NSData
选项?
I am attempting to upload a file using Alamofire
. The upload works fine when using a File (NSUrl
), however, I cant seem to figure out how to use the NSData
option?
这是我的测试:
var url:NSURL = NSURL.URLWithString("http://localhost:8080/bike.jpeg")
var err: NSError?
var imageData :NSData = NSData.dataWithContentsOfURL(url,options: NSDataReadingOptions.DataReadingMappedIfSafe, error: &err)
Alamofire.upload(.POST, "http://localhost:8080/rest/service/upload/test.png", imageData)
.progress { (bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) in
println(totalBytesWritten)
}
.responseJSON { (request, response, JSON, error) in
println(request)
println(response)
println(JSON)
}
我收到状态码 415?
I am getting a status code 415?
另外,如何在上传时发送额外的参数?
Also, how can I send across additional params in the upload?
谢谢
编辑
我没有设置正确的内容类型:
I wasn't setting the correct Content-Type:
var manager = Manager.sharedInstance
manager.session.configuration.HTTPAdditionalHeaders = ["Content-Type": "application/octet-stream"]
let imageData: NSMutableData = NSMutableData.dataWithData(UIImageJPEGRepresentation(imageTest.image, 30));
Alamofire.upload(.POST, "http://localhost:8080/rest/service/upload?attachmentName=file.jpg", imageData)
.progress { (bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) in
println(totalBytesWritten)
}
.responseString { (request, response, JSON, error) in
println(request)
println(response)
println(JSON)
}
仍然无法弄清楚如何在上传的同时发送额外的参数.
Still cant figure out how to send additional parameters along with the upload.
这里是一个简单的函数,需要目标上传 url、参数和 imageData 并返回 Alamofire.upload 上传带有参数的图像所需的 URLRequestConvertible 和 NSData.
Here is a simple function that requires the target upload url, parameters, and imageData and returns the URLRequestConvertible and NSData that Alamofire.upload requires to upload an image with parameters.
// this function creates the required URLRequestConvertible and NSData we need to use Alamofire.upload
func urlRequestWithComponents(urlString:String, parameters:Dictionary<String, String>, imageData:NSData) -> (URLRequestConvertible, NSData) {
// create url request to send
var mutableURLRequest = NSMutableURLRequest(URL: NSURL(string: urlString)!)
mutableURLRequest.HTTPMethod = Alamofire.Method.POST.rawValue
let boundaryConstant = "myRandomBoundary12345";
let contentType = "multipart/form-data;boundary="+boundaryConstant
mutableURLRequest.setValue(contentType, forHTTPHeaderField: "Content-Type")
// create upload data to send
let uploadData = NSMutableData()
// add image
uploadData.appendData("
--(boundaryConstant)
".dataUsingEncoding(NSUTF8StringEncoding)!)
uploadData.appendData("Content-Disposition: form-data; name="file"; filename="file.png"
".dataUsingEncoding(NSUTF8StringEncoding)!)
uploadData.appendData("Content-Type: image/png
".dataUsingEncoding(NSUTF8StringEncoding)!)
uploadData.appendData(imageData)
// add parameters
for (key, value) in parameters {
uploadData.appendData("
--(boundaryConstant)
".dataUsingEncoding(NSUTF8StringEncoding)!)
uploadData.appendData("Content-Disposition: form-data; name="(key)"
(value)".dataUsingEncoding(NSUTF8StringEncoding)!)
}
uploadData.appendData("
--(boundaryConstant)--
".dataUsingEncoding(NSUTF8StringEncoding)!)
// return URLRequestConvertible and NSData
return (Alamofire.ParameterEncoding.URL.encode(mutableURLRequest, parameters: nil).0, uploadData)
}
以下是如何使用它的示例(请参阅创建并发送请求):
Here's an example of how to use it (see CREATE AND SEND REQUEST):
// init paramters Dictionary
var parameters = [
"task": "task",
"variable1": "var"
]
// add addtionial parameters
parameters["userId"] = "27"
parameters["body"] = "This is the body text."
// example image data
let image = UIImage(named: "177143.jpg")
let imageData = UIImagePNGRepresentation(image)
// CREATE AND SEND REQUEST ----------
let urlRequest = urlRequestWithComponents("http://example.com/uploadText/", parameters: parameters, imageData: imageData)
Alamofire.upload(urlRequest.0, urlRequest.1)
.progress { (bytesWritten, totalBytesWritten, totalBytesExpectedToWrite) in
println("(totalBytesWritten) / (totalBytesExpectedToWrite)")
}
.responseJSON { (request, response, JSON, error) in
println("REQUEST (request)")
println("RESPONSE (response)")
println("JSON (JSON)")
println("ERROR (error)")
}
如果您需要目标 url 的 php 文件(在同一目录中有一个上传"文件夹):
And if you need the php file for the target url (with an 'uploads' folder in the same directory):
// get picture variables
$file = $_FILES['file']['tmp_name'];
$fileName = $_FILES['file']['name'];
$fileType = $_FILES['file']['type'];
// check extension
$allowedExts = array("jpg", "jpeg", "png");
$rootName = reset(explode(".", $fileName));
$extension = end(explode(".", $fileName));
// create new file name
$time = time();
$newName = $rootName.$time.'.'.$extension;
// temporarily save file
$moved = move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/".$newName );
if ($moved) $path = "uploads/".$newName;
$body = $_POST['body'];
$userId = $_POST['userId'];
$time = time();
if ($moved) {
$fullUrl = "http://antiblank.com/testPhotoUpload/".$path;
$arrayToSend = array('status'=>'success','time'=>$time,'body'=>$body,'userId'=>$userId, "imageURL"=>$fullUrl);
} else {
$arrayToSend = array('status'=>'FAILED','time'=>$time,'body'=>$body,'userId'=>$userId);
}
header('Content-Type:application/json');
echo json_encode($arrayToSend);