在nativescript-background-http中将多个图像/文件上传到PHP $ _FILE

时间:2018-09-26 11:46:48

标签: javascript php ios nativescript

我正试图将多个图像(也只有一个)存储到服务器,从nativescript-background-http发送到PHP服务器的应用程序。想要在$ _FILES中获取数据(流明$ request-> file())。 $ _FILES中的文件描述是正常的,但是大小为0,保存的文件也是如此。

{
  "fileToUpload": {
    "name": "1537960393957.png",
    "type": "image\/png",
    "tmp_name": "\/Applications\/MAMP\/tmp\/php\/phpquIq75",
    "error": 0,
    "size": 0
  }
}

Threse是无法正常工作的PHP和NS代码 PHP:

$uploadedData = [];
        $host = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]";
        /** @var UploadedFile $file */
        foreach ($request->file() as $key => $file) {
            $uploadedFile = $file->move($this->_uploadDIR, $file->hashName());
            $uploadedData[] = array(
                "name" => $uploadedFile->getFilename(),
                "url" => $host . DIRECTORY_SEPARATOR . "uploads" . DIRECTORY_SEPARATOR . $uploadedFile->getFilename()
            );
        }
        return response()->json([
            "message" => null,
            "code" => SUCCESS,
            "data" => $uploadedData
        ]);

Vanilla(JS)原始脚本:

let session = backgroundHttp.session("images-upload");
        this.addHeader("Content-Type", "application/octet-stream");
        //this.addHeader("File-name", "meno1.png");
        let request = {
            url: this.address + apiField,
            method: "POST",
            headers: this.headers
        };
        let paramsFiles = [
            {name: "test", value: "value"},
            {name: "fileToUpload", filename: "file://" + this.body[0].data, mimeType: 'image/png'}
        ];
        return session.multipartUpload(paramsFiles, request);

0 个答案:

没有答案