1

我使用jquery form來上傳沒有頁面刷新的圖像。以下是html標記:自動文件上傳使用jQuery表單提交在IE瀏覽器中拋出訪問被拒絕錯誤

<a href="javascript:void(0);" onclick="UpdateImage();"> 
<img src="@_fullImgPath" name="img-uploader" alt="" title="" /></a> 

    @using (Html.BeginForm("UpdateImage", "Profile", FormMethod.Post, new { id = "img-file-upload-form", @class = "form", autocomplete = "off", enctype = "multipart/form-data" })) 
    { 
     @Html.Hidden("UpdateImageId") 
     <input type="file" name="file" id="file" /> 
    } 
    <script language="javascript" type="text/javascript"> 
    $(function() { 
     $("#img-file-upload-form").ajaxForm({ 
      iframe: true, 
      dataType: "json", 
      beforeSubmit: function() { 
      }, 
      success: function (result) { 
       alert('done'); 
      }, 
      error: function (xhr, textStatus, errorThrown) { 
       alert('error'); 
      } 
     }); 

     $("#file").change(function() { 
      $("#img-file-upload-form").submit(); 
     }); 
    }); 

    function UpdateImage(id) { 
      $("input[id=file]").click(); 
     } 

    </script> 

這在Firefox和Chrome中運行良好,但在IE 8和9中,它會引發訪問被拒絕錯誤。 有沒有解決這個問題?或者有沒有頁面刷新上傳圖像的替代方法?

回答

0

你正在試圖在表單上保存圖像提交。提交表單將做網頁回發,因爲我猜你的標記代表MVC-3 Razor

我寧願建議你使用下面的ajax請求。

var submitForm=new Form(); 

添加您的圖像轉換成表單控件

$.ajax({ 
    url:'webpage or your method in controller', 
    type:'POST/GET', 
    data:submitForm, 
    dataType:'', 
    ContentType:'', 
    beforeSend:function(){}, 
    complete:function(){}, ..... 
}); 

用於創建動態表單標籤,你可以參考以下鏈接
How to create a form dynamically using JavaScript?

+0

避免頁面刷新,我使用的是給ajaxForm。它會照顧ajax的實現。我使用的代碼除了在IE中工作。 – Prasad

相關問題