开发者

Jquery and Input File

How do you set the value of an input file using jquery?

$('input[type="file"]').val(value) just doesn't seem to work开发者_运维知识库.

Any thoughts?


jquery uses javascript. You can't set/change the file path in a file input using javascript.


Your syntax is correct, however you can't set a value of a file input - this would be a huge security issue.


I have tried the similar things. But it's not feasible. But don't worry there is another way to do it. Here it is-

Suppose you have a input type in a div like -

 <div id="fileContainer1"><input type="file" size="52" id="reqFile1" name="reqFile1" onchange="javascript:anyMethod();"/></div>

First you have to remove the input type like below

var outer = divId; // Id of the div that contains the input type

outer.removeChild(outer.getElementsByTagName('input')[0]);

And again append it -

outer.appendChild(tempFile = document.createElement('input'));
tempFile.setAttribute('type', 'file', 0);

You can also add attribute of input type as follows

tempFile.setAttribute('id', 'reqFile1', 0);
tempFile.setAttribute('onchange',"javascript:anyMethod();", 0);
tempFile.setAttribute('size', 52);

Hope this will solve your problem what you are looking for.


You cannot set the value of an input-file. It's read-only.


Impossible because of security reasons

This is by no means possible, because it would pose a severe security threat. It's on users to decide whether they trust the site to upload sensitive files or not.

Example

Imagine dynamically setting file path (as you suggest) to some known file like some system file that holds user's preferences or something system security related.

This kind of security flaw would make browsers hackers heaven.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜