开发者

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

开发者 https://www.devze.com 2023-04-02 02:03 出处:网络
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.

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" />
                <开发者_StackOverflow中文版;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."


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


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.

0

精彩评论

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