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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里