Ioniv v4 csv file transfer to a server

Hi, I want to upload a CSV file to a server using the FileTransfer plugin using ionic v4. I am doing it but every time I have the following error

An error has occurred: Code = 1

I derive the csv file through
<input id=“file-upload-clienti” type=“file” accept=".csv" (change)=“fileUploadClienti($event)”>

and in the fileUploadClienti($event) I am doing the following

let options: FileUploadOptions = {
fileKey: ‘file’,
fileName: ‘test.csv’,
chunkedMode: false,
mimeType: “text/csv”,
headers : {Connection: “close”},
}

fileTransfer.upload(url, ‘http://xxx.xxx.xxx.xxx:8080/YYY/uploadNew.php’, options)
.then(
() => alert(“pippo”),
(e)=> {
alert("An error has occurred: Code = " + e.code);
alert(“errore source:”+e.source);
alert(“errore target:”+e.target);

                          }
                        
                      );

What I am doing wrong?

I’ve checked out the documentation of the native plugin (file-transfer). You pass as first parameter of fileTransfer.upload() a url, but I can’t see it defined in your code. Could it be possible, that this var is null or something else, so that the plugin doesn’t know, where the file is located locally?

Cheers
Unkn0wn0x

Can you suggest me a way to retrieve the right url? It is a csv file selected using the input html tag in the chrome browser.

Thanks a lot

Without knowing your code, try this to get the full local path, which should be uploaded to the server:

protected fileUploadClienti($event): void {

	const localFilePath: string = $event.target.files[0];

	let options: FileUploadOptions = {
		fileKey: ‘file’,
		fileName: ‘test.csv’,
		chunkedMode: false,
		mimeType: “text/csv”,
		headers : {Connection: “close”},
	}
		
	fileTransfer.upload(localFilePath, ‘http://xxx.xxx.xxx.xxx:8080/YYY/uploadNew.php’, options).then(() => {
		alert(“pippo”);
		return;
	}).catch((error: Error) => {
		alert("An error has occurred: Code = " + error.code);
		alert(“errore source:”+error.source);
		alert(“errore target:”+error.target);
		return;
	});
}

Hope it works! :slight_smile:

Cheers
Unkn0wn0x

I tried your code and now I have the following error

TypeError: Wrong type for parameter “filePath” of FileTransfer.upload: Expected String, but got File.

Is there a way to retrieve the correct path of the file read with . ?

Thanks

Try to console.log(localFilePath); and check, what you get as response.

Maybe a blob is returned but I don’t think so.

Cheers
Unkn0wn0x

in the log you suggest, I have the following
local path:[object File]

How Can I take its real local path?

Regards

What does the [object File] contains?

Could you post a screenshot of it?

Another way could be the native Plugin called “File Opener”. Maybe you can take a look into it:

Hope it helps!

Cheers
Unkn0wn0x