2011-05-16 39 views
0

我使用JQuery AjaxUpload上傳圖像,並嘗試使用上傳的圖像更新圖像控件,但代碼無法工作,甚至無法設置ViewState。我想知道它爲什麼會發生。下面是代碼:服務器控件在JQuery之後未更新AjaxUpload

**Javascript:**<br> 
<script type="text/javascript"> /*<![CDATA[*/<br> 
     $(document).ready(function() {<br> 
      /* Example 1 */<br> 
      var button = $('#button1'), interval;<br> 
      new AjaxUpload(button, {<br> 
       action: 'fileupload.aspx',<br> 
       name: 'myfile',<br> 
       onSubmit: function(file, ext) {<br> 
        // change button text, when user selects file<br> 
        button.text('Uploading');<br> 
        // If you want to allow uploading only 1 file at time,<br> 
        // you can disable upload button<br> 
        // this.disable();<br> 
        // Uploding -> Uploading. -> Uploading...<br> 
        interval = window.setInterval(function() {<br> 
         var text = button.text();<br> 
         if (text.length < 13) {<br> 
          button.text(text + '.');<br> 
         } else {<br> 
          button.text('Uploading');<br> 
         }<br> 
        }, 200);<br> 
       },<br> 
       onComplete: function(file, response) {<br> 
        button.text('Upload');<br> 
        window.clearInterval(interval);<br> 
        // enable upload button<br> 
        this.enable();<br> 
        // add file to the list<br> 
        $('<li></li>').appendTo('#example1 .files').text(file);<br> 
//     document.getElementById('img').src = 'C:\\Documents and <br>Settings\\Kavita\\My Documents\\Visual Studio 2008\\WebSites\\WebSite2\\ajaxUpload\\' + file;<br> 
//     alert(document.getElementById('img').src);<br> 
       }<br> 
      });<br> 
     }); /*]]>*/</script><br><br> 

fileupload.aspx.cs代碼

HttpPostedFile hpfFile = Request.Files["myfile"]; 
    if (hpfFile != null) 
    { 
     hpfFile.SaveAs(Server.MapPath("~/ajaxUpload/" + hpfFile.FileName)); 
     img.Src = Server.MapPath("~/ajaxUpload/" + hpfFile.FileName); 
    }<br> 

請讓我知道爲什麼 「img.Src」 沒有更新?

回答

1

img.Src在服務器端進行更新,但是當您進行Ajax調用時,不會將其發送回客戶端。

如果要顯示圖像,必須在onComplete jQuery函數中完成,因爲該代碼將在客戶端執行,當圖像被完全上傳時。

我還注意到服務器方法的錯誤:您使用Server.MapPath設置圖像源,這是錯誤的,因爲它會發送服務器的路徑到圖像(即「C:\ inetpub ...」),你應該只將該值設置爲:

img.Src = "~/ajaxUpload/" + hpfFile.FileName; 
相關問題