This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Dynamically set value of a file input
I have an input file HTML form and I want to set the initial value for the file path in an HTML form . I try to change tag "value" but it doesn't work anyway.
<input type="file" name="testcase" value= "C:\test.txt">
Please give me some advice for this small problem. Thanks.
From HTMLHelp.com :
The file input type creates a field
through which users can upload files
from their local computer or network.
The VALUE attribute specifies the name
of the initial file, but it is
typically ignored by browsers as a
security precaution.
Therefore, setting an initial value is not supported.
Related
This question already has answers here:
How to hide form code from view code/inspect element browser?
(14 answers)
Closed last year.
I have an input that is hidden and I don't want it to be shown in inspect element. Is there any possible way to do that?
No, there is not. Any HTML element will be accessible through the HTML source, even if generated dynamically through javascript.
If you are trying to secure some information which the client should not have access to, do not send it to the client.
i have 2 issue about file input hope that you guys can help .
1- select the text of file input field :
<input type="file" name="userfile" id="userfile" >
usually i do so by using : document.getElementById("userfile").value
but it doesnt work with Files .
2- update field value of file input. same as above , ususally i use :
document.getElementById("userfile").value = 'C:\Users\me\Pictures\pic.jpg';
i want that picture to be the selected value of a file input , but i cant do so .
anyhelp please .. ?
As stated, you can't specify a file from JavaScript that points to the user's file system. The browser won't allow you to do so. If this was possible, people would be exposing their files to websites, which could upload their private data without users knowing about it.
For your second question, if you want to access the image that the user has selected, you can use this Bootstrap plugin to get to the image data from a file input once the user has specified a file.
This question already has answers here:
How to set file input value when dropping file on page? [duplicate]
(1 answer)
Dynamically set value of a file input [duplicate]
(4 answers)
How to set a value to a file input in HTML?
(10 answers)
Closed 9 years ago.
Note:
The answers & comments below reflect the state of legacy browsers in 2009. Now you can actually set the value of the file input element dynamically/programatically using JavaScript in 2017.
See the answer in this question for details as well as a demo:How to set file input value programatically (i.e.: when drag-dropping files)?
I have 2 input type="file"
<input type="file" id="attach_file_1" name="attach_file_1" />
<input type="file" id="attach_file_2" name="attach_file_2" />
after "choosing" file for attach_file_1, there is possible to set same value for attach_file_2 using javascript ?
I don't think you can. I believe it's a security issue. You can't set the value of a file input type. Otherwise you could hide the file input element and upload a file to your server without the user's knowledge.
One way of achieving the desired functionality is to add a flag in form and mark it true when you want to copy the file information to second input. Then, on the server, you can check for that flag and use the file from first input to do whatever you wish to do with the second input.
Given that you want another input with the same value, it does appear to be possible using .cloneNode(), at least in Firefox. So far, Chrome behaves differently.
http://jsbin.com/ohafom/2/
Also note that I haven't actually tested the upload. As far as I know, Firefox will only send one. Still interesting that the clone worked.
The only way I can figure out to achieve that (but works only to "pass" the file from one to another) is to dynamically switch the input field position and names so that the file looks like it have passed from one to another.
Otherwise, it's security issue, most browsers won't let you manipulate file inputs at all.
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Putting placeholder attribute on file type input field
i want to show something like this..
I have one file-upload control and need to show some value in default file-upload control text box. how it is possible.
Can you try using
http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/TextBoxWatermark/TextBoxWatermark.aspx
because it works for sure with textbox but i have not tried with file upload control.
This question already has answers here:
How to set file input value when dropping file on page? [duplicate]
(1 answer)
How to set File objects and length property at FileList object where the files are also reflected at FormData object?
(1 answer)
Closed 6 years ago.
Is there a way to set the value of a file input (<input type="file" />) or is that all blocked for security? I'm trying to use google gears' openFiles to make a simple multi-uploader.
Note:
The answer(s) below reflect the state of legacy browsers in 2009. Now you can actually set the value of the file input element dynamically/programatically using JavaScript in 2017.
See the answer in this question for details as well as a demo:How to set file input value programatically (i.e.: when drag-dropping files)?
It is not possible to dynamically change the value of a file field, otherwise you could set it to "c:\yourfile" and steal files very easily.
However there are many solutions to a multi-upload system. I'm guessing that you're wanting to have a multi-select open dialog.
Perhaps have a look at http://www.plupload.com/ - it's a very flexible solution to multiple file uploads, and supports drop zones e.t.c.
I am working on an angular js app, andhavecome across a similar issue. What i did was display the image from the db, then created a button to remove or keep the current image. If the user decided to keep the current image, i changed the ng-submit attribute to another function whihc doesnt require image validation, and updated the record in the db without touching the original image path name. The remove image function also changed the ng-submit attribute value back to a function that submits the form and includes image validation and upload. Also a bit of javascript to slide the into view to upload a new image.
I ended up doing something like this for AngularJS in case someone stumbles across this question:
const imageElem = angular.element('#awardImg');
if (imageElem[0].files[0])
vm.award.imageElem = imageElem;
vm.award.image = imageElem[0].files[0];
And then:
if (vm.award.imageElem)
$('#awardImg').replaceWith(vm.award.imageElem);
delete vm.award.imageElem;
I had similar problem, then I tried writing this from JavaScript and it works! : referenceToYourInputFile.value = "" ;