2017-01-09 47 views
0

我正在使用asyncfileupload控件,我只想接受圖像。我使用了accept屬性,但它沒有工作,並且使用OnClientUploadComplete事件來捕獲內容類型並停止在服務器端上傳,事件沒有觸發並導致OnUploadedComplete不能啓動!而且我不知道如何阻止它在服務器端上傳。我試圖檢查OnUploadedComplete事件中的contentType,它觸發並轉到'其他',但它沒有顯示它應該顯示的消息。任何建議?使AsyncFileUpload只接受圖像文件Asp.Net Webforms

C#代碼:

`protected void fileUpload_UploadedComplete(object sender, AjaxControlToolkit.AsyncFileUploadEventArgs e) 
    { 
     if (fuImage.HasFile) 
     { 
      string contentType = fuImage.ContentType; 
      if (contentType == "image/jpeg" || contentType == "image/gif" 
       || contentType == "image/png") 
      { 
       string fileName = Path.GetFileName(e.FileName); 
       string fileUploadPath = Path.Combine(Server.MapPath("~/images/Resturant"), fileName); 
       fuImage.SaveAs(fileUploadPath); 

       var url = "~/images/Resturant/" + fileName; 
       ScriptManager.RegisterStartupScript(this, this.GetType(), "fileName", 
         "top.$get(\"" + hdfImagePath.ClientID + "\").value = '" + url + "';", true); 
      } 
      else 
      { 
      ucMessage1.MessageTitle = "Error Message"; 
      ucMessage1.MessageDetail = "Please Choose a valid image!"; 
      ucMessage1.MessageImage = "X"; 
      ucMessage1.Show(); 
     } 
    } 
}` 

設計:

<ajaxToolkit:AsyncFileUpload ID="fuImage" runat="server" OnUploadedComplete="fileUpload_UploadedComplete" /> 

回答

0

上,當文件被上傳成功,這意味着它不能檢查文件的擴展名被上傳前,服務器端的UploadedComplete事件觸發。

+0

那麼,我可以在此事件中回滾上傳嗎?甚至改變控制背景? –

+0

@RadwaAbdEllah我認爲你可以,因爲你有文件名和路徑。所以你可以檢查格式服務器端,如果它不是你想要的,刪除圖像(文件和數據庫記錄,如果有的話)。 – Mahdi

+0

現在,如果文件格式不正確,我想將控件的'backColor'更改爲'red'。我在if循環中寫了這個,但它沒有改變任何東西,你能幫我嗎? 'fuImage.BackColor = System.Drawing.Color.Red;' –

相關問題