【问题标题】:Angular 5 upload cropped file to PHP backendAngular 5 将裁剪后的文件上传到 PHP 后端
【发布时间】:2018-09-05 13:09:19
【问题描述】:

我有以下功能,可以让用户用相机拍照,或者从图库中选择它,然后裁剪图像,然后我需要使用 POST 和 multipart/form-data 将其上传到服务器 我使用了很多上传方法,但似乎都没有。

public async takePicture(sourceType) {

    // Create options for the Camera Dialog
    const options = {
        quality: 100,
        sourceType: sourceType,
        saveToPhotoAlbum: false,
        correctOrientation: true,
    };

    try {
        // Get the data of an image
        const imagePath = await this.camera.getPicture(options);

        // Special handling for Android library
        let uploadedImage;
        if (this.platform.is("android") && sourceType === this.camera.PictureSourceType.PHOTOLIBRARY) {
            const filePath = await this.filePath.resolveNativePath(imagePath);
            const correctPath = filePath.substr(0, filePath.lastIndexOf("/") + 1);
            const currentName = imagePath.substring(imagePath.lastIndexOf("/") + 1, imagePath.lastIndexOf("?"));
            uploadedImage = await this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
        } else {
            const currentName = imagePath.substr(imagePath.lastIndexOf("/") + 1);
            const correctPath = imagePath.substr(0, imagePath.lastIndexOf("/") + 1);
            uploadedImage = await this.copyFileToLocalDir(correctPath, currentName, this.createFileName());
        }

        console.log('PATH', cordova.file.dataDirectory + uploadedImage);
        this.crop.crop(cordova.file.dataDirectory + uploadedImage, {quality: 75}).then(
            newImage => {
                this.uploadedImage = newImage.replace("file://", "");

                // UPLOAD HERE
            },
            error => {
                this.presentToast("Error while selecting image.");
            }
        );
    } catch (err) {
        this.presentToast("Error while selecting image.");
    }
}


public async copyFileToLocalDir(namePath, currentName, newFileName): Promise<string> {
    const externalStoragePath: string = cordova.file.dataDirectory;
    try {
        const entry = await this.file.resolveLocalFilesystemUrl(namePath + currentName);
        const dirEntry: any = await this.file.resolveLocalFilesystemUrl(externalStoragePath);

        entry.copyTo(dirEntry, newFileName, () => { }, () => {
            this.presentToast("Error while storing file.");
        });

        return newFileName;
    } catch (error) {
        this.presentToast("Error while storing file.");
    }
}

我的文件位于类似 file:///var/mobile/Containers/Data/Application/1FF313F5-5736-44D0-968D-37889E3ED537/Library/NoCloud/1536106729187.jpg 或 /var/mobile/ 的路径中。 ../tmp/something.jpg

我已尝试通过以下方式上传它们:

const options = {} as any; // Set any options you like
const formData = new FormData();

// Append files to the virtual form.
const file = new File();
formData.append("fsFile", this.uploadedImage, "yes");

// Send it.
this.httpClient.post("/files/upload_tmp", formData, options)
                    .toPromise()
                    .catch((e) => {
                        console.log(e);
                    });

但是 formData 需要一个 blob,而不是路径,所以这不起作用。我可以使用该路径进行上传,或者如何将其转换为 blob?

谢谢

【问题讨论】:

    标签: angular file-upload angular5 httpclient


    【解决方案1】:

    Cordova 建议使用https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file-transfer/ 将文件从手机传输到服务器。这是由于跨平台支持和一些调整以使事情正常工作。

    那里有“FileUploadOptions”,您可以在其中将文件路径(设备上的本地路径)放入选项中。

    例子:

    // !! Assumes variable uploadedImage contains a valid URL to a text file on the device,
    //    for example, cdvfile://localhost/persistent/path/to/file.txt
    var options = new FileUploadOptions();
    options.fileKey = "file";
    options.fileName = uploadedImage.substr(uploadedImage.lastIndexOf('/') + 1);
    options.mimeType = "text/plain";
    
    var ft = new FileTransfer();
    ft.upload(uploadedImage, encodeURI("http://some.server.com/upload.php"), SUCCESS_FUNCTION, ERROR_FUNCTION, options)
    

    【讨论】:

    • 据我所知,这已被弃用.. 我真的不想使用已弃用的东西
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-13
    • 2019-11-29
    • 2021-09-01
    • 2011-11-11
    • 1970-01-01
    相关资源
    最近更新 更多