gpt4 book ai didi

php - 使用 Alamofire Swift 5 上传文件有问题吗?

转载 作者:行者123 更新时间:2023-11-29 13:51:45 24 4
gpt4 key购买 nike

我想上传一个简单的文件,使用

pod 'Alamofire', '~> 5.0.0-rc.3'

但我在 linux 主机的文件夹中看不到该文件。

upload.php 文件:

<?php
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size =$_FILES['image']['size'];
$file_tmp =$_FILES['image']['tmp_name'];
$file_type=$_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));

$extensions= array("jpeg","jpg","png");

if(in_array($file_ext,$extensions)===false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}

if($file_size > 2097152){
$errors[]='File size must be excately 2 MB';
}

if(empty($errors)==true){
move_uploaded_file($file_tmp,"images/".$file_name);
echo "Success";
}else{
print_r($errors);
}
}
?>

Swift 文件:

    func upload(image: UIImage,
progressCompletion: @escaping (_ percent: Float) -> Void,
completion: @escaping (_ result: Bool) -> Void) {
guard let imageData = image.jpegData(compressionQuality: 0.5) else {
print("Could not get JPEG representation of UIImage")
return
}

AF.upload(
multipartFormData: { multipartFormData in
multipartFormData.append(imageData,
withName: "imagefile",
fileName: "image.jpg",
mimeType: "image/jpeg")
},
to: "http://website.com/upload.php", usingThreshold: UInt64.init(), method: .post)

.uploadProgress { progress in
progressCompletion(Float(progress.fractionCompleted))
}
.response { response in
debugPrint(response)
}

}

@IBAction func getStartedBtnClicked(_ sender: Any) {

upload(
image: UIImage(named: "uploadFile.png")!,
progressCompletion: { [weak self] percent in
guard let _ = self else {
return
}
print("Status: \(percent)")
},
completion: { [weak self] result in
guard let _ = self else {
return
}
})
}

当用于:Swift 代码中的区域为“https://httpbin.org/post”时:

Status: 1.0
...
[Data]: 19034 bytes
[Network Duration]: 0.9285140037536621s
[Serialization Duration]: 0.0s
[Result]: success(Optional(19034 bytes))

对于我的自定义网站的 upload.php 结果是:

Status: 1.0
...
[Data]: None
[Network Duration]: 0.3820209503173828s
[Serialization Duration]: 0.0s
[Result]: success(nil)

即使是来自 Alamofire 的最简单的 block :

        if let fileURL = Bundle.main.url(forResource: "uploadFile", withExtension: "png") {
AF.upload(fileURL, to: "http://website.com/upload.php").responseJSON { response in
debugPrint(response)
}
}

我收到 inputDataNilOrZeroLength 错误:

[Request Body]: 
None
[Response]:
[Status Code]: 200
[Headers]:
Connection: Upgrade, Keep-Alive
Content-Length: 0
Content-Type: text/html; charset=UTF-8
Date: Wed, 18 Dec 2019 06:19:32 GMT
Keep-Alive: timeout=5
Server: Apache
Upgrade: h2,h2c
Vary: User-Agent
X-Powered-By: PHP/7.2.20
[Response Body]:
None
[Data]: None
[Network Duration]: 0.33376002311706543s
[Serialization Duration]: 0.0014129877090454102s
[Result]: failure(Alamofire.AFError.responseSerializationFailed(reason: Alamofire.AFError.ResponseSerializationFailureReason.inputDataNilOrZeroLength))

在 upload.php 的顶部使用以下页面从网络上运行:

<?php
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_size =$_FILES['image']['size'];
$file_tmp =$_FILES['image']['tmp_name'];
$file_type=$_FILES['image']['type'];
$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));

$extensions= array("jpeg","jpg","png");

if(in_array($file_ext,$extensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}

if($file_size > 2097152){
$errors[]='File size must be excately 2 MB';
}

if(empty($errors)==true){
move_uploaded_file($file_tmp,"images/".$file_name);
echo "Success";
}else{
print_r($errors);
}
}
?>
<html>
<body>

<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit"/>
</form>

</body>
</html>

即使是简单的文件上传,我也找不到方法。那一点我错过了什么?如果有人解释它会很棒。

稍后,我也应该配置它以上传视频文件。有什么配置建议吗?

最佳答案

您在服务器中查找的文件名是 $_FILES['image'] 并且您随请求发送的文件名是 imagefile。尝试更改 multipartFormData 追加如下

multipartFormData.append(imageData,
withName: "image",
fileName: "image.jpg",
mimeType: "image/jpeg")

关于php - 使用 Alamofire Swift 5 上传文件有问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59386634/

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