jQuery-File-Upload icon indicating copy to clipboard operation
jQuery-File-Upload copied to clipboard

How to set value of fileInput control from another fileinput control and start uploading file, on button click

Open vijendrapatil opened this issue 14 years ago • 1 comments

I am using JQuery File Upload JS.

This is a sample code

 <input id="replaceFileCnt" type="file" onchange="replaceFile(this);" />
 <div id="fileupload">


<div class="fileupload-buttonbar">
    <label class="fileinput-button">
        <span>Add files...</span>
        <input id="fileBrowse" type="file" name="files[]" multiple="multiple" />
    </label>
    <button type="button" class="delete button">
        Delete all files</button>
        <input type="checkbox" id="chkIsPublic" runat="server" /> <span style="font-weight:normal;">Is Public</span>
    <div class="fileupload-progressbar">
    </div>
</div>
<div class="fileupload-content">
    <table class="files">
    </table>
</div>

<script id="template-upload" type="text/x-jquery-tmpl">
<!-- Template definition -->
</script>
<script id="template-download" type="text/x-jquery-tmpl">
<tr class="template-download{{if error}} ui-state-error{{/if}}">
    {{if error}}            
        <td class="name">${name}</td>
        <td class="size">${sizef}</td>
        <td class="error" colspan="3">Error:
            {{if error === 1}}File exceeds upload_max_filesize (php.ini directive)
                           {{else}}${error}
            {{/if}}
        </td>
    {{else}}            
        <td class="name">
            <a href="${url}"{{if thumbnail_url}} target="_new"{{/if}}>${name}</a>
        </td>
        <td class="size">${sizef}</td>
        <td calss="dateAdded">${dateAdded}</td>
        <td calss="user">${user}</td>
        <td calss="isPublic">${isPublic}</td>
    {{/if}}
    <td class="delete">
        <button data-type="${delete_type}" data-url="${delete_url}">Delete</button> 
   </td>
    <td class="replace">
        <input type="button" onclick="browseFile();" value="Replace" />                                      
    </td>        
</tr>
</script>
</div>


  function browseFile() {

$("#replaceFileCnt").click();

return true;
   }

 function replaceFile(replaceFileCnt) {
$("#fileBrowse").val($("#replaceFileCnt").val());
/* How to start file upload ? the $('#fileupload').add(); doesnt work */
$('#fileupload').data().submit(); doesnt work 
return true;
}

Now in the above example after clicking on the Replace button, performs "click" of replaceFileCnt control which opens up the Browse dialog, and on selecting the file, the "replaceFile" function will get called. In this function i am setting the value of fileBrowse control to the value of replaceFileCnt. But this doesnt start actual upload of the file.

Can anyone help me on this?

Notice that there is areason, why i used replaceFileCnt. The reason is that, i dont want to open a dialog which allows selecting multiple files, when i click on the Replace button.

Thanks

vijendrapatil avatar Mar 06 '12 07:03 vijendrapatil

Hi, it's

$('#fileupload').fileupload('add', {files: filesList});

not

$('#fileupload').fileupload.add(...)

and...

$('#fileupload').fileupload('send', {files: filesList});

not

$('#fileupload').data().submit();

check this https://github.com/blueimp/jQuery-File-Upload/wiki/API

it's useless in any browser, through any means to programatically change the value of a file input, it's not analogue to clicking on the control and selecting a file, it just changes the name of the file being displayed

Namidas avatar Jun 18 '12 12:06 Namidas