How to get input file 's data in old version browser - javascript

I use <input type="file"/> to upload files and want to get file's message when I choose a file.
I can read it in console use:
console.log($("input[type=file]"));
but I don't know how to get .files here:
for example,I want to get file size:95786 ,how to get it using JQuery?
update:
this.files[0].size dosen't work in IE7,I want more compatible function.

Simple, you can use the querySelector() method to access the DOM elements.
This is how you should do it -
var files = document.querySelector("input[type=file]").files;
console.log(files);

<input type="file" id="myFile" />
try the following:
//binds to onchange event of your input field
$('#myFile').bind('change', function() {
//this.files[0].size gets the size of your file.
console.log(this.files[0].size);
});
Old browsers support
Be aware that old browsers will return a null value for the previous this.files call, so accessing this.files[0] will raise an exception and you should check for File API support before using it

You can get the file size using
$('input[type=file]').bind('change', function() {
console.log(this.files[0].size);
});

I always recommend checking out the jQuery File Upload plugin. It enables easy access to loads of information about uploads, including live upload speeds, bytes uploaded so far, and total upload size.

Related

On Windows and with React: What can I do to get the full path of a browsed file? [duplicate]

How to get full path of file while selecting file using <input type=‘file’>
<input type="file" id="fileUpload">
<script type="text/javascript">
function getFilePath(){
$('input[type=file]').change(function () {
var filePath=$('#fileUpload').val();
});
}
</script>
but the filePath var contains only name of selected file, not the full path.
I searched it on net, but it seems that for security reasons browsers (FF,chrome) just give name of file.
Is there any other way to get full path of selected file?
For security reasons browsers do not allow this, i.e. JavaScript in browser has no access to the File System, however using HTML5 File API, only Firefox provides a mozFullPath property, but if you try to get the value it returns an empty string:
$('input[type=file]').change(function () {
console.log(this.files[0].mozFullPath);
});
https://jsfiddle.net/SCK5A/
So don't waste your time.
edit: If you need the file's path for reading a file you can use the FileReader API instead. Here is a related question on SO: Preview an image before it is uploaded.
Try This:
It'll give you a temporary path not the accurate path, you can use this script if you want to show selected images as in this jsfiddle example(Try it by selectng images as well as other files):-
JSFIDDLE
Here is the code :-
HTML:-
<input type="file" id="i_file" value="">
<input type="button" id="i_submit" value="Submit">
<br>
<img src="" width="200" style="display:none;" />
<br>
<div id="disp_tmp_path"></div>
JS:-
$('#i_file').change( function(event) {
var tmppath = URL.createObjectURL(event.target.files[0]);
$("img").fadeIn("fast").attr('src',URL.createObjectURL(event.target.files[0]));
$("#disp_tmp_path").html("Temporary Path(Copy it and try pasting it in browser address bar) --> <strong>["+tmppath+"]</strong>");
});
Its not exactly what you were looking for, but may be it can help you somewhere.
You cannot do so - the browser will not allow this because of security concerns.
When a file is selected by using the input type=file object, the value
of the value property depends on the value of the "Include local
directory path when uploading files to a server" security setting for
the security zone used to display the Web page containing the input
object.
The fully qualified filename of the selected file is returned only
when this setting is enabled. When the setting is disabled, Internet
Explorer 8 replaces the local drive and directory path with the string
C:\fakepath\ in order to prevent inappropriate information disclosure.
And other
You missed ); this at the end of the change event function.
Also do not create function for change event instead just use it as below,
<script type="text/javascript">
$(function()
{
$('#fileUpload').on('change',function ()
{
var filePath = $(this).val();
console.log(filePath);
});
});
</script>
You can't.
Security stops you for knowing anything about the filing system of the client computer - it may not even have one! It could be a MAC, a PC, a Tablet or an internet enabled fridge - you don't know, can't know and won't know. And letting you have the full path could give you some information about the client - particularly if it is a network drive for example.
In fact you can get it under particular conditions, but it requires an ActiveX control, and will not work in 99.99% of circumstances.
You can't use it to restore the file to the original location anyway (as you have absolutely no control over where downloads are stored, or even if they are stored) so in practice it is not a lot of use to you anyway.
Did you mean this?
$('#i_file').change( function(event) {
var tmppath = URL.createObjectURL(event.target.files[0]);
$("img").fadeIn("fast").attr('src',tmppath);
});
You can use the following code to get a working local URL for the uploaded file:
<script type="text/javascript">
var path = (window.URL || window.webkitURL).createObjectURL(file);
console.log('path', path);
</script>
One interesting note: although this isn't available in on the web, if you're using JS in Electron then you can do this.
Using the standard HTML5 file input, you'll receive an extra path property on selected files, containing the real file path.
Full docs here: https://github.com/electron/electron/blob/master/docs/api/file-object.md
You can, if uploading an entire folder is an option for you
<input type="file" webkitdirectory directory multiple/>
change event will contain:
.target.files[...].webkitRelativePath: "FOLDER/FILE.ext"
but it doesn't contain the whole absolute path, only the relative one. Supported in Firefox also.
you should never do so... and I think trying it in latest browsers is useless(from what I know)... all latest browsers on the other hand, will not allow this...
some other links that you can go through, to find a workaround like getting the value serverside, but not in clientside(javascript)
Full path from file input using jQuery
How to get the file path from HTML input form in Firefox 3
One could use the FileReader API for changing the src of an img element.
https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL
This is a working solution for me
const path = (window.URL || window.webkitURL).createObjectURL(file);
It will return a blob URL to locally access the file.
file element has and array call files it contain all necessary stuff you need
var file = document.getElementById("upload");
file.addEventListener("change", function() {
for (var i = 0; i < file.files.length; i++) {
console.log(file.files[i].name);
}
}, false);
You can get the full path of the selected file to upload only by IE11 and MS Edge.
var fullPath = Request.Form.Files["myFile"].FileName;
$('input[type=file]').change(function () {
console.log(this.files[0].path);
});
This is the correct form.

Pass files from input-element to plupload (Add folder)

I want to upload all files of a folder. Since plupload does not offer this functionality, I have an input like this
<input type="file" name="files[]" id="files" multiple directory="" webkitdirectory="" mozdirectory="">
that stores all files out of a chosen folder. I now want to load these to my uploader
uploader = new plupload.Uploader({});
When I try to do so like
$('#files').change(function (){
if ($(this).prop('files').length > 0)
{
uploader.addFile($('#files').prop('files')[0]);
}
});
it results in
Uncaught TypeError: Cannot read property 'processing' of null
at
uploader.start();
How can I pass file references from my input-element to the plupload-Uploader?
I got a workaround here. One set the files of the plupload input-element via jQuery and trigger onChange to make it work.
$('#html5_plupload-HASH').prop('files', $('#files').get(0).files).change();
I also discovered that plupload actually offers support for webkitdirectory in version 2 according to their list of supported features. There is a parameter called select_folder but I could not get it to work by now.
It is also possible to just find the input-element in plupload.full(.min).js and add webkitdirectory attribute.

Browse "File Upload" not displaying full path through Javascript [duplicate]

How to get full path of file while selecting file using <input type=‘file’>
<input type="file" id="fileUpload">
<script type="text/javascript">
function getFilePath(){
$('input[type=file]').change(function () {
var filePath=$('#fileUpload').val();
});
}
</script>
but the filePath var contains only name of selected file, not the full path.
I searched it on net, but it seems that for security reasons browsers (FF,chrome) just give name of file.
Is there any other way to get full path of selected file?
For security reasons browsers do not allow this, i.e. JavaScript in browser has no access to the File System, however using HTML5 File API, only Firefox provides a mozFullPath property, but if you try to get the value it returns an empty string:
$('input[type=file]').change(function () {
console.log(this.files[0].mozFullPath);
});
https://jsfiddle.net/SCK5A/
So don't waste your time.
edit: If you need the file's path for reading a file you can use the FileReader API instead. Here is a related question on SO: Preview an image before it is uploaded.
Try This:
It'll give you a temporary path not the accurate path, you can use this script if you want to show selected images as in this jsfiddle example(Try it by selectng images as well as other files):-
JSFIDDLE
Here is the code :-
HTML:-
<input type="file" id="i_file" value="">
<input type="button" id="i_submit" value="Submit">
<br>
<img src="" width="200" style="display:none;" />
<br>
<div id="disp_tmp_path"></div>
JS:-
$('#i_file').change( function(event) {
var tmppath = URL.createObjectURL(event.target.files[0]);
$("img").fadeIn("fast").attr('src',URL.createObjectURL(event.target.files[0]));
$("#disp_tmp_path").html("Temporary Path(Copy it and try pasting it in browser address bar) --> <strong>["+tmppath+"]</strong>");
});
Its not exactly what you were looking for, but may be it can help you somewhere.
You cannot do so - the browser will not allow this because of security concerns.
When a file is selected by using the input type=file object, the value
of the value property depends on the value of the "Include local
directory path when uploading files to a server" security setting for
the security zone used to display the Web page containing the input
object.
The fully qualified filename of the selected file is returned only
when this setting is enabled. When the setting is disabled, Internet
Explorer 8 replaces the local drive and directory path with the string
C:\fakepath\ in order to prevent inappropriate information disclosure.
And other
You missed ); this at the end of the change event function.
Also do not create function for change event instead just use it as below,
<script type="text/javascript">
$(function()
{
$('#fileUpload').on('change',function ()
{
var filePath = $(this).val();
console.log(filePath);
});
});
</script>
You can't.
Security stops you for knowing anything about the filing system of the client computer - it may not even have one! It could be a MAC, a PC, a Tablet or an internet enabled fridge - you don't know, can't know and won't know. And letting you have the full path could give you some information about the client - particularly if it is a network drive for example.
In fact you can get it under particular conditions, but it requires an ActiveX control, and will not work in 99.99% of circumstances.
You can't use it to restore the file to the original location anyway (as you have absolutely no control over where downloads are stored, or even if they are stored) so in practice it is not a lot of use to you anyway.
Did you mean this?
$('#i_file').change( function(event) {
var tmppath = URL.createObjectURL(event.target.files[0]);
$("img").fadeIn("fast").attr('src',tmppath);
});
You can use the following code to get a working local URL for the uploaded file:
<script type="text/javascript">
var path = (window.URL || window.webkitURL).createObjectURL(file);
console.log('path', path);
</script>
One interesting note: although this isn't available in on the web, if you're using JS in Electron then you can do this.
Using the standard HTML5 file input, you'll receive an extra path property on selected files, containing the real file path.
Full docs here: https://github.com/electron/electron/blob/master/docs/api/file-object.md
You can, if uploading an entire folder is an option for you
<input type="file" webkitdirectory directory multiple/>
change event will contain:
.target.files[...].webkitRelativePath: "FOLDER/FILE.ext"
but it doesn't contain the whole absolute path, only the relative one. Supported in Firefox also.
you should never do so... and I think trying it in latest browsers is useless(from what I know)... all latest browsers on the other hand, will not allow this...
some other links that you can go through, to find a workaround like getting the value serverside, but not in clientside(javascript)
Full path from file input using jQuery
How to get the file path from HTML input form in Firefox 3
One could use the FileReader API for changing the src of an img element.
https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL
This is a working solution for me
const path = (window.URL || window.webkitURL).createObjectURL(file);
It will return a blob URL to locally access the file.
file element has and array call files it contain all necessary stuff you need
var file = document.getElementById("upload");
file.addEventListener("change", function() {
for (var i = 0; i < file.files.length; i++) {
console.log(file.files[i].name);
}
}, false);
You can get the full path of the selected file to upload only by IE11 and MS Edge.
var fullPath = Request.Form.Files["myFile"].FileName;
$('input[type=file]').change(function () {
console.log(this.files[0].path);
});
This is the correct form.

How to get selected files actual path instead of fake or temporary file path? [duplicate]

How to get full path of file while selecting file using <input type=‘file’>
<input type="file" id="fileUpload">
<script type="text/javascript">
function getFilePath(){
$('input[type=file]').change(function () {
var filePath=$('#fileUpload').val();
});
}
</script>
but the filePath var contains only name of selected file, not the full path.
I searched it on net, but it seems that for security reasons browsers (FF,chrome) just give name of file.
Is there any other way to get full path of selected file?
For security reasons browsers do not allow this, i.e. JavaScript in browser has no access to the File System, however using HTML5 File API, only Firefox provides a mozFullPath property, but if you try to get the value it returns an empty string:
$('input[type=file]').change(function () {
console.log(this.files[0].mozFullPath);
});
https://jsfiddle.net/SCK5A/
So don't waste your time.
edit: If you need the file's path for reading a file you can use the FileReader API instead. Here is a related question on SO: Preview an image before it is uploaded.
Try This:
It'll give you a temporary path not the accurate path, you can use this script if you want to show selected images as in this jsfiddle example(Try it by selectng images as well as other files):-
JSFIDDLE
Here is the code :-
HTML:-
<input type="file" id="i_file" value="">
<input type="button" id="i_submit" value="Submit">
<br>
<img src="" width="200" style="display:none;" />
<br>
<div id="disp_tmp_path"></div>
JS:-
$('#i_file').change( function(event) {
var tmppath = URL.createObjectURL(event.target.files[0]);
$("img").fadeIn("fast").attr('src',URL.createObjectURL(event.target.files[0]));
$("#disp_tmp_path").html("Temporary Path(Copy it and try pasting it in browser address bar) --> <strong>["+tmppath+"]</strong>");
});
Its not exactly what you were looking for, but may be it can help you somewhere.
You cannot do so - the browser will not allow this because of security concerns.
When a file is selected by using the input type=file object, the value
of the value property depends on the value of the "Include local
directory path when uploading files to a server" security setting for
the security zone used to display the Web page containing the input
object.
The fully qualified filename of the selected file is returned only
when this setting is enabled. When the setting is disabled, Internet
Explorer 8 replaces the local drive and directory path with the string
C:\fakepath\ in order to prevent inappropriate information disclosure.
And other
You missed ); this at the end of the change event function.
Also do not create function for change event instead just use it as below,
<script type="text/javascript">
$(function()
{
$('#fileUpload').on('change',function ()
{
var filePath = $(this).val();
console.log(filePath);
});
});
</script>
You can't.
Security stops you for knowing anything about the filing system of the client computer - it may not even have one! It could be a MAC, a PC, a Tablet or an internet enabled fridge - you don't know, can't know and won't know. And letting you have the full path could give you some information about the client - particularly if it is a network drive for example.
In fact you can get it under particular conditions, but it requires an ActiveX control, and will not work in 99.99% of circumstances.
You can't use it to restore the file to the original location anyway (as you have absolutely no control over where downloads are stored, or even if they are stored) so in practice it is not a lot of use to you anyway.
Did you mean this?
$('#i_file').change( function(event) {
var tmppath = URL.createObjectURL(event.target.files[0]);
$("img").fadeIn("fast").attr('src',tmppath);
});
You can use the following code to get a working local URL for the uploaded file:
<script type="text/javascript">
var path = (window.URL || window.webkitURL).createObjectURL(file);
console.log('path', path);
</script>
One interesting note: although this isn't available in on the web, if you're using JS in Electron then you can do this.
Using the standard HTML5 file input, you'll receive an extra path property on selected files, containing the real file path.
Full docs here: https://github.com/electron/electron/blob/master/docs/api/file-object.md
You can, if uploading an entire folder is an option for you
<input type="file" webkitdirectory directory multiple/>
change event will contain:
.target.files[...].webkitRelativePath: "FOLDER/FILE.ext"
but it doesn't contain the whole absolute path, only the relative one. Supported in Firefox also.
you should never do so... and I think trying it in latest browsers is useless(from what I know)... all latest browsers on the other hand, will not allow this...
some other links that you can go through, to find a workaround like getting the value serverside, but not in clientside(javascript)
Full path from file input using jQuery
How to get the file path from HTML input form in Firefox 3
One could use the FileReader API for changing the src of an img element.
https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL
This is a working solution for me
const path = (window.URL || window.webkitURL).createObjectURL(file);
It will return a blob URL to locally access the file.
file element has and array call files it contain all necessary stuff you need
var file = document.getElementById("upload");
file.addEventListener("change", function() {
for (var i = 0; i < file.files.length; i++) {
console.log(file.files[i].name);
}
}, false);
You can get the full path of the selected file to upload only by IE11 and MS Edge.
var fullPath = Request.Form.Files["myFile"].FileName;
$('input[type=file]').change(function () {
console.log(this.files[0].path);
});
This is the correct form.

SCRIPT5: Access is denied. [duplicate]

We want to reduce the number of steps it takes for a user to upload a file on our website; so we're using jQuery to open and postback files using the below markup (simplified):
<a onclick="$('#uplRegistrationImage').click();">
Change profile picture
</a>
<!-- Hidden to keep the UI clean -->
<asp:FileUpload ID="uplRegistrationImage"
runat="server"
ClientIDMode="static"
Style="display:none"
onchange="$('#btnSubmitImage').click();" />
<asp:Button runat="server"
ID="btnSubmitImage"
ClientIDMode="static"
Style="display:none"
OnClick="btnSubmitImage_OnClick"
UseSubmitBehavior="False" />
This works absolutely fine in Firefox and Chrome; opening the file dialog when the link is clicked and firing the postback when a file is selected.
However in IE9 after the file upload has loaded and a user has selected a file; insteaed of the OnChange working I get a "SCRIPT5 Access is denied" error. I've tried setting an arbitrary timeout, setting intervals to check if a file is given to no avail.
There are a number of other questions relating to this; however none appear to have a decent answer (One said set the file dialog to be transparent and hover behind a button!)
Has anyone else resolved this? Or is it absolutely necessary that I provide a button for IE users?
For security reasons, what you are trying to do is not possible. It seems to be the IE9 will not let you submit a form in this way unless it was an actual mouse click on the File Upload control that triggers it.
For arguments sake, I was able to use your code to do the submit in the change handler, but it worked only when I clicked the Browse button myself. I even set up polling in the $(document).ready method for a variable set by the change handler that indicates a submission should be triggered - this didn't work either.
The solutions to this problem appear to be:
Styling the control in such a way that it sits behind a button. You mentioned this in your question, but the answer provided by Romas here In JavaScript can I make a "click" event fire programmatically for a file input element? does in fact work (I tried in IE9, Chrome v23 and FF v15).
Using a Flash-based approach (GMail does this). I tried out the Uploadify demo and it seems to work quite nicely.
Styling a File Upload:
http://www.quirksmode.org/dom/inputfile.html
http://www.shauninman.com/archive/2007/09/10/styling_file_inputs_with_css_and_the_dom
References:
jQuery : simulating a click on a <input type="file" /> doesn't work in Firefox?
IE9 file input triggering using Javascript
getting access is denied error on IE8
Hey this solution works.
for download we should be using MSBLOB
$scope.getSingleInvoicePDF = function(invoiceNumberEntity) {
var fileName = invoiceNumberEntity + ".pdf";
var pdfDownload = document.createElement("a");
document.body.appendChild(pdfDownload);
AngularWebService.getFileWithSuffix("ezbillpdfget",invoiceNumberEntity,"pdf" ).then(function(returnedJSON) {
var fileBlob = new Blob([returnedJSON.data], {type: 'application/pdf'});
if (navigator.appVersion.toString().indexOf('.NET') > 0) { // for IE browser
window.navigator.msSaveBlob(fileBlob, fileName);
} else { // for other browsers
var fileURL = window.URL.createObjectURL(fileBlob);
pdfDownload.href = fileURL;
pdfDownload.download = fileName;
pdfDownload.click();
}
});
};
This solution looks like it might work. You'll have to wrap it in a <form> and get it to post in the jquery change handler, and probably handle it in form_load using the __eventtarget or and iframe or whatever it is that web forms uses, but it allows you to select a file, and by submitting the form, it should send it. I can't test it however, since I don't have an environment set up at home.
http://jsfiddle.net/axpLc/1/
<a onclick="$('#inputFile').click();">
Change profile picture
</a>
<div id='divHide'>
<input id='inputFile' type='file' />
</div>
$('#inputFile').change(function() { alert('ran'); });
#divHide { display:none; }
Well, like SLC stated you should utilize the <Form> tag.
First you should indicate the amount of files; which should be determined by your input fields. The second step will be to stack them into an array.
<input type="file" class="upload" name="fileX[]"/>
Then create a loop; by looping it will automatically be determined based on the input field it's currently on.
$("input[#type=file]:nth(" + n +")")
Then you'll notice that each file chosen; will replace the input name to the file-name. That should be a very, very basic way to submit multiple files through jQuery.
If you'd like a single item:
$("input[#type=file]").change(function(){
doIt(this, fileMax);
});
That should create a Div where the maximum file found; and attaches to the onEvent. The correlating code above would need these also:
var fileMax = 3;
<input type="file" class="upload" name="fileX[]" />
This should navigate the DOM parent tree; then create the fields respectively. That is one way; the other way is the one you see above with SLC. There are quite a few ways to do it; it's just how much of jQuery do you want manipulating it?
Hopefully that helps; sorry if I misunderstood your question.

Categories

Resources