Add extra params to $cordovaFileTransfer.upload

2019-07-08 10:12发布

问题:

I am using $cordovaFileTransfer.upload() to upload a picture from a device to my server. In documentation it says that it excepts these params: server, filePath, options. I need to pass in some extra custom params: like user_id, for example. Does anyone know if it's possible to do? Thank you.

回答1:

You can send those custom parameters in the options object, doing something like this:

var options = {};
options.headers = {
    Connection: "close"
}
options.chunkedMode = false;
options.userName = 'Mati';

$cordovaFileTransfer.upload(server, filePath, options)


回答2:

You can add "params" to options object, for example :

 var options = {
                fileKey: "name",
                fileName: "image.png",
                chunkedMode: false,
                mimeType: "image/png",
                httpMethod: "post",  //here the methed (get, post,..) 
                params: $scope.Data  //here you can submit the data
 };


回答3:

You need to use the 'params' property, like this:

var _options = {
    fileKey: "image_name",
    fileName: "image.jpg",
    chunkedMode: false,
    mimeType: "image/jpeg",
    httpMethod: "POST",
    headers: { },
    params: {
        "extra_param_name": "Jamilson",
        "extra_param_last_name": "Junior"
    }
};

Hope this helps.



回答4:

var options = new FileUploadOptions();
options.fileKey = "image";
options.fileName="image.jpg";
options.mimeType = "image/jpeg";
options.httpMethod="POST";
options.headers = {
    Connection: "close"
};
options.chunkedMode = false;
var params = {};
params.user_id ="123";
params.username="username";
options.params = params;