I need to get the path of a file that I select in the JavaScript. What I researched, the file.value parameter should return this path. But it is returning undefined.
How to solve?
Below part of the code:
var fileInput = document.getElementById('fileInput');
var file = fileInput.files[0];
var path = file.value;
document.getElementById("path").value = path; // ---> IT IS SHOWING "undefined"
fileInput.files[0] is a File object. It doesn't have a value property.
See: JavaScript File API
Also, for security reasons you can't get the full path of a file, just the filename.
Related
I'm building an online csv converter that allows a user to upload a csv file and download the processed csv output file. Everything works fine, except the dowloaded file has a name that looks like "6fd665aa-74d7-4b4e-96e1-38aea0cca9e6.csv" (it changes every time) that has nothing to do with the input file's name.
How can I change this downloaded file name ?
const processedStr = convertCSV(text);
const myBlob = new Blob([processedStr], {type : 'text/csv'});
dllink.href = window.URL.createObjectURL(myBlob);
dllink.click();
You can try to add an attribute to the dllink variable. It will give a name to the download attribute and hence the file.
const processedStr = convertCSV(text);
const myBlob = new Blob([processedStr], {type : 'text/csv'});
dllink.href = window.URL.createObjectURL(myBlob);
dllink.setAttribute("download","custom_name.csv"); // Added Line
dllink.click();
can anyone one help how to read the first line of .txt file? I have input type file that upload example.txt then I would like to grab the first line from that code. I tried something like this:
<input type="file" id="fileUpload" name="fileUpload"/> MyTest.txt //file name
function confirmFileSubmit(){
var fileName = $('#fileUpload').val();
alert($('#fileUpload').split('\n')[0]);
}
After I run my code this just outputted file name in alert box. I'm not sure how I can read the content of the file. If anyone can help please let me know.
You'll need the FileReader for that
function confirmFileSubmit(){
var input = document.getElementById('fileUpload'); // get the input
var file = input.files[0]; // assuming single file, no multiple
var reader = new FileReader();
reader.onload = function(e) {
var text = reader.result; // the entire file
var firstLine = text.split('\n').shift(); // first line
console.log(firstLine); // use the console for debugging
}
reader.readAsText(file, 'UTF-8'); // or whatever encoding you're using
// UTF-8 is default, so this argument
} // is not really needed
I am new in javascript and was trying to read and write in local txt file so I did as I found in the web but for some reason it stucks in new File instantiation:
<script type="text/javascript">
var txtFile = "d:/test.txt"
alert('point 1');
var file = new File(txtFile);
alert('point 2');
</script>
It prints only 'point 1' string. Thanks.
new File() constructor requires at least two parameters; first parameter an array containing String, ArrayBufferView, ArrayBuffer Blob or another File object; the second the file name. You cannot specify a download directory for files using javascript, but you can set a download directory at browser settings or preferences. You can also utilize download attribute at a element to set file name at Save File dialog.
I am new in javascript and was trying to read and write in local txt
file
You could use <input type="file"> element to retrieve test.txt, and edit .txt file at <textarea> before re-saving with same file name. See also Edit, save, self-modifying HTML document; format generated HTML, JavaScript , How to Write in file (user directory) using JavaScript?
<script type="text/javascript">
var txtFile = "test.txt";
var text = "abc";
var file = new File([text], txtFile);
console.log(file);
var a = document.createElement("a");
a.download = file.name;
a.href = URL.createObjectURL(file);
document.body.appendChild(a);
a.innerHTML = file.name;
a.onclick = function() {
this.parentElement.removeChild(this)
}
</script>
I am trying to check the file type of files that I get off Google drive, how ever the file type always seem to be "" how would I get file types e.g. "text/plain"
var fileInput = document.getElementById('file');
var file = fileInput.files[0];
var textType = "text.*";
var textTypePDF = "application/pdf";
then I call
file.type.match(textType);
But the file.type is always equal to "", any one had this before?
here i have variable fileVal takes path of file like uploads/import/abc.pdf
var fileVal = $('#fileimport').val();
here fileVal takes path of file like uploads/import/abc.pdf
i have to send this file path as url variable to a php file
and then display result on messagediv.
like
$('#messagediv').load('../xyz.php?file='+fileVal);
here without url variable, its working perfect getting values to the messagediv.
But where as using url variable its not.
How i can solve this?
try with encodeURIComponent()
var fileVal = encodeURIComponent($('#fileimport').val());
That should work fine except you have a syntax error
// $var fileVal = $('#fileimport').val();
var fileVal = $('#fileimport').val();