2012-06-09 34 views
0

我使用这些代码在根目录文件夹上传两张图片,我已经给出了两个按钮上传BUTTON2我可以上传图像的单独image.on点击并且在图像控制中显示该图像,并且我也对点击按钮2做了同样的处理。但在这里,我想要获取我上传的图片的路径,使用Button2和button3点击button1(比较)。如何获得在按钮变量上传图片的路径的值单击

这里是我的代码,我使用: 我已经试过这对Button1的Click功能:但它没有显示任何值。

我应该做在两个字符串变量文件名1和文件名2获得价值?

 //string filename1 = FileUpload1.PostedFile.FileName; 
     //Response.Write(filename1); 
     //string filename2 = FileUpload2.PostedFile.FileName; 
     //Response.Write(filename2); 

ASPX页面代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Day_8_campairTwoImageUpload.aspx.cs" Inherits="validate.Day_8_campairTwoImageUpload" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:FileUpload ID="FileUpload1" runat="server" /> 
     <asp:Button ID="Button2" runat="server" 
      Text="upload" onclick="Button2_Click" /><asp:Label ID="StatusLabel" runat="server" Text="Status"></asp:Label> 
     <br /><br /> 
     <asp:FileUpload ID="FileUpload2" runat="server" /><asp:Button ID="Button3" 
      runat="server" Text="upload" onclick="Button3_Click" /><asp:Label ID="StatusLabel1" runat="server" 
       Text="Status"></asp:Label><br /><br /> 

    <asp:Button ID="Button1" runat="server" Text="Compar" a onclick="Button1_Click" /> 
    </div> 
    <asp:Image ID="Image1" runat="server" Height="100" Width="100" />&nbsp;&nbsp;&nbsp; 
    <asp:Image ID="Image2" runat="server" Height="100" Width="100" /> 
    </form> 
</body> 
</html> 

ASPX.cs页面代码:

//BUTTON2=CODE TO UPLOAD FIRST IMAGE AND SHOW IT IN IMAGE CONTROL 
     protected void Button2_Click(object sender, EventArgs e) 
     { 

      if (FileUpload1.HasFile) 
      { 
       try 
       { 
        if (FileUpload1.PostedFile.ContentType == "image/jpeg") 
        { 
         if (FileUpload1.PostedFile.ContentLength < 102400) 
         { 
          //EnsureDirectoriesExist(); 

          string filename1 = Path.GetFileName(FileUpload1.FileName); 
          FileUpload1.SaveAs(Server.MapPath(@"~/upload/") + filename1); 
          StatusLabel.Text = "Upload status: File uploaded!"; 

          Image1.ImageUrl ="/Upload/"+FileUpload1.FileName.ToString(); 


          // string filename1 = Server.MapPath(@"~/upload/") + FileUpload1.FileName; 
          // Response.Write(filename1); 
         } 
         else 
          StatusLabel.Text = "Upload status: The file has to be less than 100 kb!"; 
        } 
        else 
         StatusLabel.Text = "Upload status: Only JPEG files are accepted!"; 
       } 
       catch (Exception ex) 
       { 
        StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message; 
       } 
      } 
     } 
//BUTTON3=CODE TO UPLOAD SECOND IMAGE AND SHOW IT IN IMAGE CONTROL 
     protected void Button3_Click(object sender, EventArgs e) 
     { 
      if (FileUpload2.HasFile) 
      { 
       try 
       { 
        if (FileUpload2.PostedFile.ContentType == "image/jpeg") 
        { 
         if (FileUpload2.PostedFile.ContentLength < 102400) 
         { 
          //EnsureDirectoriesExist(); 

          string filename2 = Path.GetFileName(FileUpload2.FileName); 
          FileUpload2.SaveAs(Server.MapPath(@"~/upload/") + filename2); 
          StatusLabel1.Text = "Upload status: File uploaded!"; 
          Image2.ImageUrl = "/Upload/" + FileUpload2.FileName.ToString(); 
         } 
         else 
          StatusLabel1.Text = "Upload status: The file has to be less than 100 kb!"; 
        } 
        else 
         StatusLabel1.Text = "Upload status: Only JPEG files are accepted!"; 
       } 
       catch (Exception ex) 
       { 
        StatusLabel1.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message; 
       } 
      } 


     } 

//BUTTON1=CODE TO GET THE PATH NAME OF BOTH UPLOADED IMAGE IN TWO VARIABLE 


protected void Button1_Click(object sender, EventArgs e) 
     { 


      // string filename1 = FileUpload1.PostedFile.FileName; 
      //Response.Write(filename1); 
      // string filename2 = FileUpload2.PostedFile.FileName; 
      //Response.Write(filename2); 


     } 

     } 
    } 
+0

想得到的上传路径/保存的文件? – adatapost

+0

@AVD肯定的,但在的button1_Click(对象发件人,EventArgs的) – Kango

回答

0

您可以使用ViewState中/会话/ HiddenField保存在Click处理上传文件的路径按钮2和按钮3。

protected void Button2_Click(object sender, EventArgs e) 
{ 
    if (FileUpload1.HasFile) 
    { 
     ..... 
     ViewState["file1"]=Server.MapPath("~/upload/" + FileUpload1.FileName); 
     .... 
    } 
} 

protected void Button3_Click(object sender, EventArgs e) 
{ 
    if (FileUpload2.HasFile) 
    { 
     ..... 
     ViewState["file2"]=Server.MapPath("~/upload/" + FileUpload2.FileName); 
     .... 
    } 
} 

在的button1_Click处理程序,

protected void Button1_Click(object sender, EventArgs e) 
{ 
    if(ViewState["file1"]!=null) 
     Label1.Text=ViewState["file1"].ToString(); 
    if(ViewState["file2"]!=null) 
     Label2.Text=ViewState["file2"].ToString(); 
} 
+0

谢谢...... AVD及其著作的功能 – Kango

0

它出来,你未保存的图片网址的任何地方,例如在数据库或诸如会话和等临时变量, 所以您应该在数据库或在临时变量中这些图像的URL保存如会话,并在您BUTTON3点击事件中使用保存的数据,希望它帮助你