我现在要做一个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.有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!
{
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();
}
但是像这样的写法
<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表单读入,这样图片还是不显示!
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
流内会多出这些东西,怎么把他去掉!
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");
}
}
}
<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());
}
}
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效果是一样的!
Response.Redirect(Request.UrlReferrer.ToString() + "?1=1&");
所以想在b页面post回a页面这样就不用显示了,可以实现吗?