Mangesh
Mangesh

Reputation: 3997

FileUpload control using Html.BeginForm/Ajax.BeginForm does not work.(htmlfile access is denied in IE).Css style to upload control button

To give a CSS style to Upload Control button I created a fakebutton.On click of this fake button i trigger the click event of file upload control. When i use Jquery ajax it gives me a error htmlfile access is denied. but when i use Html.BeginForm it does not give me error nor does it work.

<div id="divUploadForm">
    @using (Html.BeginForm("UploadAction", "HomeController",new AjaxOptions()
    {
        UpdateTargetId = "divUploadForm",        
        InsertionMode = InsertionMode.Replace        
    }))
    {  
        <fieldset>                            
                <input type="text" id="fakeupload" name="fakeupload" class="fakeupload" />
                <input type="button" value="Browse" id="BrowseBtn" />
                <input type="file" name="file" id="files-to-upload" onchange="this.form.fakeupload.value = this.value;" style="display: none" />
                <input type='submit' id='upload-files' value='Save'/>            
        </fieldset>
    }
</div>
<script type="text/javascript">   
    $(function () {
        $('#BrowseBtn').click(function () {
            $('#files-to-upload').trigger('click');
        });
        $('#fakeupload').click(function () {
            $('#files-to-upload').trigger('click');
        });
    });
</script>

Update : also used

@using (Html.BeginForm("UploadAction", "HomeController", FormMethod.Post, new { enctype = "multipart/form-data", id = "UploadForm" }))
{
...
}

Still it gives the "Error:Access is denied."

Upvotes: 0

Views: 1393

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038780

You are using Html.BeginForm with AjaxOptions? There is no such overload. Only Ajax.* helpers take AjaxOptions.

This being said you cannot upload files with AJAX. So stick with a normal Html.BeginForm but make sure you specify proper enctype to multipart/form-data:

@using (Html.BeginForm("UploadAction", "HomeController", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    ...
}

If you want to use javascript to upload files and handle things like progress, etc... you may take a look at one of the numerous plugins available.

Upvotes: 1

Rafay
Rafay

Reputation: 31033

if your file upload control is hidden the browsers wont allow click on it for security reasons, plus you cannot use ajax to upload files as javascript cannot access the file contents

Upvotes: 1

Related Questions