Get server URL from file input in Javascript - javascript

I need to manage some files who are uploaded to my server, but I need to get the URL of the selected file. I know you can't do this with javascript with files outside the server, but I wanna know if there's a way to get the URL from files in the server. For example I got this image from my file input with a selected file (LTAIPBCS75FI.xls) who is hosted in "xls/ITAI2016/Fraccion01/" in my home directory. I get this URL from php:
URL IN FILE INPUT
I need then to get the full directory when I select a file in my server.

Related

Android webview - how to fetch a file from external storage

I have a mobile app that wraps around the web-app, using webview.
The web-app has a button to open a large .zip file (e.g. 100 MB).
The user clicks a button, and selects a .zip file.
This triggers an onChange function with a variable of type File (Blob), which includes attributes like:
file name
file size
file type (application/zip)
The javascript code then parses the .zip file, extracts specific data within it and uses it within the web-app.
This works well within the web-app, when the app is called via the Chrome browser.
For example when operated in chrome browser on an Android phone, I can pull the .zip file and open it in the web-app.
I want to do the same but using the mobile app.
I am able to pick up the .zip file using a File Chooser, and pass it to Webview but I have problems to fetch the file from the Javascript code.
For reference, I am able to pass an image, by creating a data_uri using stringBuilder and passing the content (as data:image/jpeg;base64).
But the zip file is much larger.
When calling fetch(fileUri) from the Javascript side I'm getting errors.
I'm using the following uri
/storage/emulated/0/Android/data/com.example/files/Download/file2.zip
The fetch succeeds but returns a blob with size of 165 (i.e. not the actual size of the file) which hosts the error message:
{
"error": "Not Found",
"message": "The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again."
}
The program flow is like so:
I select a .zip file via FileChooser.
In onActivityResult, the uri value is /document/msf:12858 (seen via uri = intent.getData();)
The uri needs to be mapped into a real path file url, such that the fileUrl will be passed to webview.
Webview will then fetch the file using the fileUrl.
I searched how to get the real path file url when selecting a file with FileChooser, and found
this, and this links.
I wasn't able to get the real file path, so I decided to read the file and write it to another location, so I can get a file path. (this is not efficient and done just to check the functionality).
I create the new file using the following code:
InputStream stream = context.getContentResolver().openInputStream(uri);
File file2 = new File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "file2.zip");
writeBytesToFile(stream, file2);
I don't see any errors when creating the file, and when creating the file, the number of bytes that are read and written to the new file are as expected.
For file2, I get a value of:
/storage/emulated/0/Android/data/com.example/files/Download/file2.zip
Then, within the Javascript code I fetch this file path.
But I'm getting a Blob with the "file-not-found" content as above.
So:
How can I verify that the file is indeed created and that the path can be fetched from webview?
How can I get the real file path of the original selected file, so I don't have to read and write the original file to new location just to get the file path?
Thanks
I was able to get the file from external storage by doing the following steps:
create an initial uri (uri1)
The uri is created by:
creating a temporary file (file1) in the storage dir via
context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)
I'm not sure why a temporary file need to be created but if I don't create a file I cannot get the uri.
createFile3
get the uri via
Uri uri1 = FileProvider.getUriForFile(context, "com.example.android.fileprovider", file1);
create an intent with the following attributes:
Intent.ACTION_OPEN_DOCUMENT
category: Intent.CATEGORY_OPENABLE
type: "application/zip"
extra attribute: fileIntent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, uri1);
this opens a dialog box for selecting openable zip files in the Downloads directory,
after the file is selected, a new uri (uri2) is created that includes the name of the selected file.
extract the name of the file via
String fileName = getFileName(context, uri2);
create the dirPath by appending the filename
dirPath = "/data/user/0/com.example/" + fileName;
if the dirPath does not exist (first time), write the file to its dirPath location.
on successive ocassions dirPath exists, so there is no need to re-write the file.
open the file with regular Java means, e.g. via
ZipFile zip = new ZipFile(dirPath);

input type file with file path

I want to import data from a path in an input type file.
It is in use in the kendo UI, and when I wrote the article, I uploaded the data to the spreadsheet by using the file upload window and saved the data to the server.
Therefore, the file is stored in the server-specified path, and DB contains basic information such as file path, file name, and extension.
When I read a post using this, it was successful to retrieve the information of the file from the DB, but it is not in the reading part of the file.
So, I do not upload the file from the input type file that I used at the time of writing, but I want to read the file information from fromfile.
If you know how to do it, please let me know.
This is the code I used to write.
<input class="icon_input_file" type="file" id="insertFile" name="insertFile"/>
$("#insertFile").on("change", function(){
var spreadsheet = $("#spreadsheet").data("kendoSpreadsheet");
spreadsheet.fromFile(this.files[0]);
});

retrieve a specific file and store

I have some php files which I am hosting on a web hosting site.I want to take a specific file from user's desktop and upload it to my server on click of a button.The user should not specify the file path and the file path is known to me.
For eg:
php code
copy("C:\Users\Admin\Desktop\IMG_20141118_213032.jpg",
"/home/u131954735/public_html/uploads/new.jpg");
where C:.... gives the path of the image file on my Desktop and /home/u13.. gives the path where I want to store the file on server as new.jpg.
But on running the above code gives :
Warning: copy(C:\Users\Admin\Desktop\IMG_20141118_213032.jpg): failed
to open stream: No such file or directory in
/home/u131954735/public_html/upload.php
How to rectify this problem?
I think that you can't copy a file from user's desktop with the url (for security problems).
You can upload file sending it to server: http://www.w3schools.com/php/php_file_upload.asp

Save a text file from PHP and AJAX

I have a file in the C: drive
'C:/myFile.txt'
I know the path and I simply want the user to save the file. However I try to use javascript
window.location.assign('C:/myFile.txt');
I get the path name not being understood.
I also try
window.open('C:/myFile.txt');
So I was thinking to send an AJAX request to PHP, supply the path, and use fopen. I do that and pretty much nothing happens. I need it to prompt as a download/save as.
If this server is remote to the user, they will not be able to download a file with a path from your C: drive in their browser.
You will need to place the file somewhere in your web root and then use its URL for downloading: window.location.assign('http://www.example.com/myFile.txt');

Change file names before uploading with SWFUpload

I am using SWFUpload to allow users to upload multiple files in any browser. A user can provide custom file names for the files being uploaded. How can I iterate through all the queued files and update the name of the file to the custom name before the file is uploaded.
If I can't change the file name, how do I add a post parameter to each file being uploaded to make the change on the server side? I know how to add parameters for all files but how would I do it for each file?
You can't update the actual name of the file that gets sent in the POST body of the file upload because internally, SWFUpload is using a FileReference which doesn't let you change any of the file's properties before uploading it (and there's no way to get proper upload progress without using a FileReference to do the uploading, so this isn't something that can really be changed).
However, you should be able to add an extra POST parameter per file via the addFileParam function. Its signature is:
addFileParam(file_id:String, name:String, value:String):Boolean

Categories