doulingzou1712 2017-03-09 13:15 采纳率: 0%
浏览 64
已采纳

图像上传发送数据但没有在接收端创建文件

I'm stuck. and lost at what to do to solve it. I'm sending a image with the methods below in Swift from a iPhone. And I can see the data is being received by the server with Wireshark. I can also see in the php-server's terminal that I'm receiving data.

it's data that I don't know how to handle.

I'm currently using the php script below to handle everything with the upload there is a lot of test variables that I'm using to just see if I could figure out what I was receiving if anything.

    public func post(data: Data, url: URL)
    {
        var request = URLRequest(url: url)
        //request.addValue("filenameTest.jpg", forHTTPHeaderField: "filename")
        request.httpMethod = "POST"
        let body = createBodyWithParameters(filePathKey: "filenameTest.jpg",data: data,boundary: generateBoundaryString())
        request.setValue("Keep-Alive", forHTTPHeaderField: "Connection")
        let configuration = URLSessionConfiguration.default
        let session = URLSession(configuration: configuration,delegate: self, delegateQueue: OperationQueue.main)
        let task = session.uploadTask(with: request as URLRequest, from: body)
        task.resume()

    }
func createBodyWithParameters(filePathKey: String?, data: Data!, boundary: String) -> Data {
        var body = Data();

        let mimetype = "image/jpg"

        body.append(Data( "--\(boundary)
".utf8))
        //body.append(Data( "Content-Disposition: form-data; name=\"\(filePathKey!)\"; filename=\"\(parameters!["filename"]!)\"
".utf8))
        body.append(Data( "Content-Disposition: form-data; filename=\"\(filePathKey!)\"
".utf8))
        body.append(Data( "Content-Type: \(mimetype)

".utf8))


        body.append(data)
        body.append(Data( "
".utf8))

        body.append(Data( "--\(boundary)--
".utf8))

        return body
    }
    func generateBoundaryString() -> String {
        return "----Boundary---"
    }

Wireshark

Requested: ??~?3?<??6??Ns?i|2?~`kxB?;??N???$_?|?9?:  

Requested: ???c?S?Y?Gn8>?5?k???y?H??3?N:???????M?_?r??q????~??O?6?Pc?]%"d??x??u?:  

Requested: ??mRKmS????:  

Requested: ????~k]?~}t?E??EO???????'???-????????$?W??|_?Wsl???f???n?:?Q????_?_???_?_2c_8?S?c?:  

Requested: ?????#?:  

Requested:  
[Thu Mar  9 13:55:51 2017] 192.168.250.101:55327 [200]: /




<?php




$target_dir = "wp-content/uploads";

if(!file_exists($target_dir))
{
mkdir($target_dir, 0777, true);
}

$target_dir = $target_dir . "/" . basename($_SERVER['HTTP_FILENAME']);




if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_dir)) {
echo json_encode([
"Message" => "The file ". basename(header('filename')). " has been uploaded.",
"Status" => "OK"
]);

} else {

echo json_encode([
"Message" => "Sorry, there was an error uploading your file.",
"Status" => "Error"

]);
}
?>

and I'm using this kind of code to figure out what variables contain what.

 $testvar = $_SERVER['HTTP_FILENAME'];
    file_put_contents("php://stdout", "
Requested : $testvar /r/n");
    $testvar = count($_REQUEST);
    $testvar = $_REQUEST[3];
    file_put_contents("php://stdout", "
Requested : $testvar /r/n");
    foreach($_FILES['file'] as $value){
    $testvar = ": ". $value;
    file_put_contents("php://stdout", "
Requested: $testvar 
");
    }

I don't know how to fix this. I know the data is there just can't seem to get a hold of it. I've tried several ways to send it with the swift part. this one plus 2 others had the same issue when I get to the php side the filename was nowhere to be found and I couldn't even find a temp name of the file.

note that the image data is never saved to a file on the iPhone and I have no intention of ever letting that happen. I simply need to take a image with the camera and send it, I've accomplished that now but when I try to save a image file on the PHP side everything becomes seemingly impossible to do.

there is no content in $_FILES as far as I can see the content is in the $_REQUEST as well as the file but when I try to access that one I just get wired signs and question marks.

  • 写回答

1条回答 默认 最新

  • doubi1928 2017-03-14 12:50
    关注

    I did solve this eventually. My problem was that I was generating a new boundary instead of using the same boundary in each request. as well as I forgot to add the multipart/form-data to the Content-Type of the request. Also notice the -- in the end of my createBodyWithParameters above.. they are apparently important I found out by accidentally removing them.

    let boundary = generateBoundaryString()
    
            var request = URLRequest(url: url)
            request.httpMethod = "POST"
            request.addValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
            let body = createHttpBody(filename:"filenametest.jpg" , objData: objData,boundary: boundary)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建