简体   繁体   English

使用 PHP 使用 Alamofire 在 Swift 5 中接收图像上传

[英]Receiving Image Upload in Swift 5 with Alamofire Using PHP

I am trying to send an image from an iOS app to a server with Swift using Alamofire's multipart form data upload.我正在尝试使用 Alamofire 的多部分表单数据上传将图像从 iOS 应用程序发送到具有 Swift 的服务器。 I am receiving the data with PHP but it doesn't work somehow.我正在使用 PHP 接收数据,但它无法以某种方式工作。 I looked for tutorials and other questions on stackoverflow, but non of them are in Swift 5, and I'm not sure which part is causing the error.我在 stackoverflow 上查找了教程和其他问题,但没有一个在 Swift 5 中,我不确定哪个部分导致了错误。

This is the Swift code in the app:这是应用程序中的 Swift 代码:

let imageData = Image!.pngData()!

AF.upload(multipartFormData: { multipartFormData in
    multipartFormData.append(imageData, withName: "image", fileName: "test.png", mimeType: "image/png")
    print("uploading image")
}, to: url).responseJSON { response in
    debugPrint(response)
}

The url is correct and the png data is not empty. url是正确的,png数据不为空。 This is the server side code in PHP (inspired by the answer to this stackoverflow question ):这是 PHP 中的服务器端代码(受此stackoverflow 问题的答案启发):

<?php

// If there is no image data
if (empty($_FILES["image"])) {
    $response = array("error" => "nodata");
}
// If there is data
else {
    $response['error'] = "NULL";

    $filename = $_FILES["image"]["name"];
    $path = "D:/emailback/images/" . $filename;

    if (move_uploaded_file($_FILES['image']['tmp_name'], $path)) {
        $response['status'] = "success";
        $response['filename'] = "".$_FILES["file"]["name"];
        $response['filepath'] = $path;

    } else {
        $response['status'] = "Failure";
        $response['error']  = "".$_FILES["image"]["error"];
        $response['name']   = "".$_FILES["image"]["name"];
        $response['path']   = "".$path;
        $response['type']   = "".$_FILES["image"]["type"]; 
        $response['size']   = "".$_FILES["image"]["size"];
    }
}

echo json_encode($response);

?>

But I in the response I'm getting suggests that $_FILES['image']['size'] is zero.但我在收到的回复中暗示$_FILES['image']['size']为零。 I'm not sure what the size means.我不确定大小是什么意思。 It also doesn't show what type it is.它也没有显示它是什么类型。 $_FILES['image']['tmp_name'] is also empty. $_FILES['image']['tmp_name']也是空的。 This is the response I'm keep getting:这是我不断收到的回复:

[Result]: success({
    error = 6;
    name = "test.png";
    path = "D:/emailback/images/test.png";
    size = 0;
    status = Failure;
    type = "";
})

I'm not quite sure what this means and how the issue can be solved.我不太确定这意味着什么以及如何解决问题。 Thanks in advance:)提前致谢:)

i think their is some mis-match in your swift as well as PHP code such as you are not defining get/post method, encodingCompletion handler in alamofire call and in PHP code you are trying to get image name of 'file' named image我认为它们在您的 swift 以及 PHP 代码中存在一些不匹配,例如您没有定义 get/post 方法,alamofire 调用中的 encodingCompletion 处理程序和 Z2FEC392304A5C23AC138DA 中的 Z2FEC392304A5C23AC138DA23AC138DA23AC138DA2284F

$response['filename'] = "".$_FILES["file"]["name"];

but their is no any file uploaded with name 'file'.但他们没有上传任何名为“文件”的文件。 so replace this line with below line所以用下面的行替换这一行

$response['filename'] = "".$_FILES["image"]["name"];

for sending image to server with an iOS app using Alamofire try the below code使用 Alamofire 使用 iOS 应用程序将图像发送到服务器,请尝试以下代码

Alamofire.upload(multipartFormData: {  multipartFormData in
                multipartFormData.append(imageData, withName: "image", fileName: "test.png", mimeType: "image/png")
        },to: url, method: .post, encodingCompletion: { encodingResult in
            switch encodingResult {
                case .success(let upload, _, _):
                    upload.responseJSON { response in
                        print(response.result)
                    }
                case .failure(let encodingError):
                    print(encodingError)
                }
        })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM