2012-09-19 59 views
5

可能重複:
How to restrict file type in FileUpload controlASP.NET文件上傳圖片,保存路徑和文件名在SQL表

我有一個問題,用我的圖片上傳。它會上傳所有類型的文件。 我需要背後的代碼,以確定它是否爲圖像(jpg,png等)。 然後它需要在我的sql中保存路徑和文件名。 保存名稱和路徑已啓動並正在運行,正則表達式也是如此。我現在需要包含我在這裏找到的som代碼。問題是,它是如何完成的?

我後面的代碼是:

protected void btnUpload_Click(object sender, EventArgs e) 
{ 
    if (FileUpload1.PostedFile != null) 
    { 
     string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName); 

     //Save files to disk 
     FileUpload1.SaveAs(Server.MapPath("~/_PublicData/Images/" + FileName)); 

     //Add Entry to DataBase 
     String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["Computer_Klubben_CommunitySiteConnectionString"].ConnectionString; 
     SqlConnection con = new SqlConnection(strConnString); 
     string strQuery = "insert into dbo.Billeder (FileName, FilePath)" + " values(@FileName, @FilePath)"; 
     SqlCommand cmd = new SqlCommand(strQuery); 
     cmd.Parameters.AddWithValue("@FileName", FileName); 
     cmd.Parameters.AddWithValue("@FilePath", "~/_PublicData/Images/" + FileName); 
     cmd.CommandType = CommandType.Text; 
     cmd.Connection = con; 

     try 
     { 
      con.Open(); 
      cmd.ExecuteNonQuery(); 
     } 

     catch (Exception ex) 
     { 
      Response.Write(ex.Message); 
     } 

     finally 
     { 
      con.Close(); 
      con.Dispose(); 
     } 
    } 
} 

我需要把這些代碼下面的代碼中,我已經在這裏找到。 How can i upload only jpeg files?

我把代碼放在這裏的代碼後面,還是放置它? 請幫忙。

回答

3
protected void btnUpload_Click(object sender, EventArgs e) 
{ 
    if (FileUpload1.PostedFile != null) 
    { 
      string fileExt = 
       System.IO.Path.GetExtension(FileUpload1.FileName); 

      if (fileExt == ".jpeg" || fileExt == ".jpg") 
      { 

string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName); 

     //Save files to disk 
     FileUpload1.SaveAs(Server.MapPath("~/_PublicData/Images/" + FileName)); 

     //Add Entry to DataBase 
     String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["Computer_Klubben_CommunitySiteConnectionString"].ConnectionString; 
     SqlConnection con = new SqlConnection(strConnString); 
     string strQuery = "insert into dbo.Billeder (FileName, FilePath)" + " values(@FileName, @FilePath)"; 
     SqlCommand cmd = new SqlCommand(strQuery); 
     cmd.Parameters.AddWithValue("@FileName", FileName); 
     cmd.Parameters.AddWithValue("@FilePath", "~/_PublicData/Images/" + FileName); 
     cmd.CommandType = CommandType.Text; 
     cmd.Connection = con; 

     try 
     { 
      con.Open(); 
      cmd.ExecuteNonQuery(); 
     } 

     catch (Exception ex) 
     { 
      Response.Write(ex.Message); 
     } 

     finally 
     { 
      con.Close(); 
      con.Dispose(); 
     } 


} 
else 
{ 
    //Show Error Message. Invalid file. 
} 


    } 

} 
+0

這就是我的想法....我現在就試試。 – Anders

+0

謝謝....我不得不聽如何首先做一個論壇。我的老師首先需要我的關注。這是我想要的。再次感謝。 – Anders

1

我發現這個解決辦法的解決方案:

<asp:FileUpload ID="fuImportImage" runat="server" /> 
<asp:RegularExpressionValidator ID="regexValidator" runat="server" 
    ControlToValidate="fuImportImage" 
    ErrorMessage="Only JPEG images are allowed" 
    ValidationExpression="(.*\.([Jj][Pp][Gg])|.*\.([Jj][Pp][Ee][Gg])$)"> 
</asp:RegularExpressionValidator> 
2

你從代碼問behing所以試試這個方法,如果他們是一些圖片或不驗證你的文件名。通過比較它們的擴展..只要通過你的FileUplaod控件的名稱,以這種方法和驗證按鈕的點擊..

private Boolean ImageUploadValidation(FileUpload UploadedFile) 
{ 
    String FileExtension = String.Empty, Code = String.Empty; 
    try 
    { 
     if (String.IsNullOrEmpty(UploadedFile.PostedFile.FileName)) 
     { 
      Code = "<script> alert(' Please select file');</script>"; 
      ClientScript.RegisterStartupScript(this.GetType(), "someKey", Code); 
      return false; 
     } 

     FileExtension = Path.GetExtension(UploadedFile.FileName).ToLower(); 

     if (!FileExtension.Equals(".gif") && 
      !FileExtension.Equals(".png") && 
      !FileExtension.Equals(".jpg") && 
      !FileExtension.Equals(".bmp") && 
      !FileExtension.Equals(".gif") && 
      !FileExtension.Equals(".jpeg") && 
      !FileExtension.Equals(".tif") && 
      !FileExtension.Equals(".tiff")) 
     { 
      Code = "<script> alert(' Please select valid file. File can be of extension(gif, png, jpg, bmp, gif, jpeg, tif, tiff)');</script>"; 
      ClientScript.RegisterStartupScript(this.GetType(), "someKey", Code); 
      return false; 
     } 
     return true; 
    } 
    catch (Exception) 
    { 

     throw; 
    } 
+0

我'對不起,我編輯你的錯誤代碼我認爲這是我的 –

+0

如何回滾我的編輯,我很抱歉再次,因爲我提出了答案,我希望修改我的答案 –

+0

這很好..我再次編輯代碼.. :) –

0

這裏是你的正則表達式..

System.Text.RegularExpressions.Regex imageFilenameRegex = new 
System.Text.RegularExpressions.Regex(@"(.*?)\.(jpg|jpeg|png|gif)$", 
System.Text.RegularExpressions.RegexOptions.IgnoreCase); 


bool ismatch =imageFilenameRegex.IsMatch(imgFile.FileName) 
相關問題