我现在要做一个asp.net的图片上传功能!需求如下:
1.有A.aspx,b.aspx两个页面;在A.aspx页面做如下代码
<form id="form1" action="B.aspx" method="post" enctype="multipart/form-data">
    <div><input id="File1" type="file" name="txtName" />
        <input type="submit" id="submit1" value="上传" name="Submit1" /></div>
</form>
页面Post到B.aspx页面,具体的上传是在B.aspx页面实现,上传完成后B.aspx会再将页面Post回来,
并且把上传时有自定义名称返回到A.aspx!

解决方案 »

  1.   

    try 
            { 
                
                string ImgPath = FileUpload1.PostedFile.FileName; 
                string ImgName = ImgPath.Substring(ImgPath.LastIndexOf("\\") + 1); 
                string ImgExtend = ImgPath.Substring(ImgPath.LastIndexOf(".") + 1); 
                if (!(ImgExtend == "bmp" ¦ ¦ ImgExtend == "jpg" ¦ ¦ ImgExtend == "gif")) 
                { 
                    Label3.Text = "上传图片的格式不正确!"; 
                    return; 
                } 
                int FileLen = this.FileUpload1.PostedFile.ContentLength; 
                Byte[] FileData = new Byte[FileLen]; 
                HttpPostedFile hp = FileUpload1.PostedFile;//创建访问客户端上传文件的对象 
                Stream sr = hp.InputStream;//创建数据流对象 
                sr.Read(FileData, 0, FileLen);//将图片数据放到FileData数组对象实例中,其中0代表数组指针的起始位置,FileLen表示要读取流的长度(指针的结素位置) 
                SqlConnection con = new SqlConnection("server=(local);user id=sa;pwd=sa;database=Book"); 
                con.Open(); 
                SqlCommand com = new SqlCommand("INSERT INTO Goods (GoodsPhoto) VALUES (@imgdata)", con); 
                com.Parameters.Add("@imgdata", SqlDbType.Image); 
                com.Parameters["@imgdata"].Value = FileData; 
                com.ExecuteNonQuery(); 
                Label3.Text = "保存成功!"; 
            } 
            catch (Exception error) 
            { 
                Label3.Text = "处理失败!原因为:" + error.ToString(); 
            } 
      

  2.   

    直接在同一个页面用iframe不就得了
      

  3.   

    干嘛要这样弄,直接在A页面不行么?要是为了通用和安全考虑,也可以写个单独的上传类嘛,这样A到B,再B到A,怎么看都合适哦。
      

  4.   

    一个页面是不行的!这只是一个初步的功能,我要再把他改为可以在两个域名之间上传的接口!就是要在www.a.com做上传其实真正的实现功能是在www.b.com上进行的!
      

  5.   

    这个并不是单个页面上传,是a.aspx将form表单内容post到b.aspx内,b.aspx真正实现的上传功能,
    但是像这样的写法
    <form id="form1" action="B.aspx" method="post" enctype="multipart/form-data"> 
        <div> <input id="File1" type="file" name="txtName" /> 
            <input type="submit" id="submit1" value="上传" name="Submit1" /> </div> 
    </form> 
    会把整个表单post过去,读成二进制流,如果再把二进制流读为图片的话就会把整个form表单读入,这样图片还是不显示!
      

  6.   

    如果把post过来的二进制数据再写成图片的话
     byte[] b = Request.BinaryRead(Request.TotalBytes);
                FileStream fs = new FileStream(Server.MapPath("ImageFile" + "\\5.jpg"), FileMode.Create);
                StreamWriter sw = new StreamWriter(fs);
                fs.Write(b, 0, b.Length);
                fs.Flush();
                fs.Close();会出现这样的问题
    -----------------------------7d81d9f20f3a
    Content-Disposition: form-data; name="txtName"; filename="C:\Documents and Settings\Administrator\桌面\好平.jpg"
    Content-Type: image/pjpeg
    流内会多出这些东西,怎么把他去掉!
      

  7.   

    响应到本页,然后读取文件保存在Session中,传递给另一个页面保存.
    A.aspx
     
    <script>
       function openDialog(Url){
         d = new Date();
         var fea="dialogHeight:600px;dialogWidth:900px;status:no;resizable:yes";
         var returnValue = window.showModalDialog(Url+"&t=" + d.getTime() ,null,fea);
         if (returnValue != null)
         {
           alert(returnValue);
         }
          return false;
        }
       
      </script></head>
    <body>
      <form id="form1" runat="server">
        <div>
          <asp:FileUpload runat="server" ID="uploadFile" />
          <asp:Button runat="server" ID="lbtCommit" Text="Upload" OnClick="lbtCommit_Clicked" />
        </div>
      </form>
    a.aspx.cs
      
    protected void lbtCommit_Clicked(object sender, EventArgs e)
      {
         Session["up"] = uploadFile.FileBytes;
         Page.ClientScript.RegisterStartupScript(this.GetType(), "open", "<script>openDialog('b.aspx?1=1');</script>");
      }
    b.aspx.cs
      
     protected void Page_Load(object sender, EventArgs e)
      {
        if (!IsPostBack)
        {
          if (Session["up"] != null)
          {
            byte[] f = (byte[])Session["up"];
            string fileName = Server.MapPath(".") + @"\aa.bmp";        FileStream fs = File.Create(fileName);
            fs.Write(f, 0, f.Length);
            fs.Close();        Page.ClientScript.RegisterStartupScript(this.GetType(), "Close", "<script>window.returnValue='"+
               fileName  +"';window.close();</script>");        Session.Remove("up");
          }
        }
      }
      

  8.   

    谢谢你了!不过这个不能用session,也不能用cookie,因为这个最后是要跨工程的!只能用post!a.aspx post到b.aspx,b上传后post回a.aspx,并将重新命名后的名字一起post返回!
      

  9.   

    直接送也简单,需要注意的是A页面的这个form要放置在页面form之外,并且不能runat=serverA.aspx<body>
      <form id="form1" runat="server">
        <div>
        </div>
      </form>
      <form id="form2" method="POST" enctype="multipart/form-data" action="b.aspx">
        <input type="file" name="upfile2" id="upfile2" />
        <input type="submit" value="Go2" />
      </form>
    </body>
    B.aspx.csprotected void Page_Load(object sender, EventArgs e)
      {
        if (!IsPostBack)
        {
          for (int i = 0; i < Request.Files.Count; i ++ )
          {
            Request.Files[i].SaveAs(Server.MapPath(".") + "/aaa.jpg");
            Response.Redirect(Request.UrlReferrer.ToString());  
          }
        }
      

  10.   

    如果我不想用Response.Redirect来跳转页面,我想让b页面执行完了就post回a页面怎样实现,要把新名字也带回来,因为我不想在url里显示消息!
      

  11.   

    string data = "新名称"
     string strUrl = Request.UrlReferrer.ToString();
                    byte[] data = Encoding.UTF8.GetBytes(guid);
                    // 准备请求...
                    HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(strUrl);
                    myRequest.Method = "POST";
                    myRequest.ContentType = "application/x-www-form-urlencoded";
                    myRequest.ContentLength = data.Length;
                    Stream newStream = myRequest.GetRequestStream();
                    // 发送数据
                    newStream.Write(data, 0, data.Length);
                    newStream.Close();
                    Response.Redirect(strUrl);
    我这样写了一个!我不知道我理解的对不对!我是想让他post回以前的页面,同时把新名字写到流里一起post回来!我在a页面怎样得到data里的新名字!或许我这样写的就是错误的!其实效果和直接Response.Redirect效果是一样的!
      

  12.   

    Url带参数试试
    Response.Redirect(Request.UrlReferrer.ToString() + "?1=1&");  
      

  13.   

    我知道这样是可以的!只是不想让传的参数值在地址栏里显示(不能用session),
    所以想在b页面post回a页面这样就不用显示了,可以实现吗?