2012-09-19 56 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) 
相关问题