gpt4 book ai didi

php - 如何将 UIImage 上传到 PHP Web API

转载 作者:行者123 更新时间:2023-11-28 08:44:58 26 4
gpt4 key购买 nike

我正在尝试通过网络 API 将 UIImage 上传到服务器。我正在使用 laravel 5.2 创建我的 web rest API。

这是我在 Swift 中的函数,它应该使用 UIImage 和一些参数向我的 API 发出发布请求:

func uploadWithAlamofire(imageUpload: UIImage) {
let image = imageUpload

// define parameters
let parameters = [
"hometown": "abc",
"living": "abc"
]

// Begin upload
Alamofire.upload(.POST, "http://localhost:8888/buyIT/public/app/api/createPost",
// define your headers here
headers: ["Authorization": "auth_token"],
multipartFormData: { multipartFormData in

// import image to request
if let imageData = UIImageJPEGRepresentation(image, 1) {
multipartFormData.appendBodyPart(data: imageData, name: "file", fileName: "myImage.png", mimeType: "image/png")
}

// import parameters
for (key, value) in parameters {
multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key)
}
},


// you can customise Threshold if you wish. This is the alamofire's default value
encodingMemoryThreshold: Manager.MultipartFormDataEncodingMemoryThreshold,
encodingCompletion: { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.responseJSON { response in
print(response)
}
case .Failure(let encodingError):
print(encodingError)
}
})
}

但我不知道如何获取图片,然后将其上传到我的服务器。

这是我在我的 web api 中尝试的:

public function createPost(Request $request)
{
$file = $request->input('file');
$input = $request->only('hometown');



//Create deals folder if not exist
if (!file_exists('Upload/images')) {
mkdir('Upload/images', 0777, true);
}

if(file_exists('Upload/images'))
{
// SET UPLOAD PATH
$destinationPath = 'Upload/images';
// GET THE FILE EXTENSION
if ($file != null) {
$extension = $file->getClientOriginalExtension();
// RENAME THE UPLOAD WITH RANDOM NUMBER
$fileName = rand(11111, 99999) . '.' . $extension;
// MOVE THE UPLOADED FILES TO THE DESTINATION DIRECTORY
$upload_success = $file->move($destinationPath, $fileName);

//$filePath = url('/').'/'.$destinationPath.'/'.$fileName;

if($upload_success)
{
return "success";

}
else {
return "failed !!!";
}
}

}
else
{
return "failed";
}


return $file;
}

最佳答案

或者,你可以使用 AlamoFire

func createMultipart(image: UIImage, callback: Bool -> Void){
// use SwiftyJSON to convert a dictionary to JSON
var parameterJSON = JSON([
"id_user": "test"
])
// JSON stringify
let parameterString = parameterJSON.rawString(encoding: NSUTF8StringEncoding, options: nil)
let jsonParameterData = parameterString!.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
// convert image to binary
let imageData = UIImageJPEGRepresentation(image, 0.7)
// upload is part of AlamoFire
upload(
.POST,
URLString: "http://httpbin.org/post",
multipartFormData: { multipartFormData in
// fileData: puts it in "files"
multipartFormData.appendBodyPart(fileData: jsonParameterData!, name: "goesIntoFile", fileName: "json.txt", mimeType: "application/json")
multipartFormData.appendBodyPart(fileData: imageData, name: "file", fileName: "iosFile.jpg", mimeType: "image/jpg")
// data: puts it in "form"
multipartFormData.appendBodyPart(data: jsonParameterData!, name: "goesIntoForm")
},
encodingCompletion: { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.responseJSON { request, response, data, error in
let json = JSON(data!)
println("json:: \(json)")
callback(true)
}
case .Failure(let encodingError):
callback(false)
}
}
)
}

let fotoImage = UIImage(named: "foto")
createMultipart(fotoImage!, callback: { success in
if success { }
})

关于php - 如何将 UIImage 上传到 PHP Web API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35579307/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com