无法上传图片(React native,Laravel)

时间:2018-08-30 18:59:46

标签: ios laravel react-native axios

我已经尝试了一段时间,但没有成功

后端

public function createInspection(Request $request) {
   $path = Storage::disk('public')->putFile('bin_images', new File($request->file('image')));
   return $path;    
}

客户

data.append("image", {
  uri: this.state.images[0].path,
  type: this.state.images[0].mime,
  size: this.state.images[0].size,
  name: filename
});

this.props.sendInspection(data)

服务(短路)

const res = await axios.post(url, data, {
    headers: {
      Authorization: `Bearer ${token}`,
      Accept: "application/json",
      "Content-Type": "multipart/form-data"
    }
  });

我不确定是否与未确认的路径有关(iOS图像路径),例如路径: /private/var/mobile/Containers/Data/Application/B6FEB0FD-F9C8-4088-B15F-99D27A818C76/tmp/react-native-image-crop-picker/7BD81594-30E0-4E00-919C-4F353BBDE46F.jpg

我收到此错误

message: "The file "" does not exist", exception: "Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException", file: "/Users/ysr/tza-project/vendor/symfony/http-foundation/File/File.php", line: 37, file: "/Users/ysr/tza-project/vendor/symfony/http-foundation/File/MimeType/MimeTypeGuesser.php", line: 116

2 个答案:

答案 0 :(得分:0)

我不知道反应如何,但似乎您仅传递图像属性,而不传递图像文件或以字节为单位的图像数据。

尝试更改您的代码:

<vue-dropzone ref="docfile" id="dropzone" :options="dzOptions"></vue-dropzone>

dzOptions: {
    url: self.$apiUrl + "client/documents/",
    autoProcessQueue: false,
    acceptedFiles: "application/pdf",
    uploadMultiple: false,
    maxNumberOfFiles: 1,
    maxFilesize: 30,
    addRemoveLinks: true,
    dictDefaultMessage: "Select File",
    init: function() {
        this.on("addedfiles", function(files) {
            if (files.length > 1) {
                self.$toaster.error("You can upload only one.");
                this.removeAllFiles();
                return;
            }
            if (files[0].type != "application/pdf") {
                self.$toaster.error("You can upload only pdf file.");
                this.removeAllFiles();
                return;
            }
            self.upload();
        });
    }
}

upload() {
    var self = this;
    if (self.$refs.docfile.dropzone.files.length == 0) {
        self.$toaster.error("No document to upload.");
        return;
    }
    var filePath = self.$refs.docfile.dropzone.files[0];
    ...
}

此代码:

data.append("image", {
  uri: this.state.images[0].path,
  type: this.state.images[0].mime,
  size: this.state.images[0].size,
  name: filename
});

或尝试将data.append("image", { file: this.state.images[0], uri: this.state.images[0].path, type: this.state.images[0].mime, size: this.state.images[0].size, name: filename }); 更改为Content-Type

如果您的图片是base64,则需要对API进行如下更改:

'multipart/form-data; boundary=${data._boundary}'

答案 1 :(得分:0)

问题似乎出在您的后端,请检查documentation

您应该执行以下操作:

public function update(Request $request)
{
    $path = $request->file('image')->store('images');

    return $path;
}

如果您想使用putFile,则可以使用:

$path = Storage::putFile('images', $request->file('image'));

,如果需要更换磁盘,则:

$path = Storage::disk('public')->putFile('images', $request->file('image'));