html表单中的图片框用什么区别,(转)html中使用表单和input file上传图片

本文转载自:http://hi.baidu.com/love_1210/item/120e452b42b2a854c38d59eb

客户端代码:

//如果file框没有加runat="server",则 form里一定要加上 enctype="multipart/form-data"这样才可以实现上传文件到服务器;使用了server和没有使用

runat="server"是有区别的.使用了runat="server"的form编译后,action必定是指向本身的网页。而没

有加runat="server"的form可以指向一个网页。

服务器端代码:

private string retvalue = "ok";

protected void Page_Load(object sender, EventArgs e)

{

if(!IsPostBack)

{

HttpPostedFile req = Request.Files["file1"];

if (req == null || req.ContentLength 

{

Response.Write("没有文件");

Response.End();

}

else

{

try

{

string extion = System.IO.Path.GetExtension(req.FileName.ToString());

string date = DateTime.Now.ToString("yyyyMMddhhmmss").ToString();

string src = date + extion;

string pathnew = Server.MapPath("~/testfile/");

req.SaveAs(pathnew+src);        //自带的方式保存文件

/*读取文件流保存

Stream stream = req.InputStream;

//string src = "test.xls";

string fullpathnew = pathnew + src;

if (!Directory.Exists(pathnew))

{

Directory.CreateDirectory(pathnew);

}

BinaryReader br = new BinaryReader(stream);

byte[] fileByte = br.ReadBytes((int)stream.Length);

// string content = fileByte.ToString();

using (FileStream fileStream = new FileStream(fullpathnew, FileMode.Create))

{

fileStream.Write(fileByte, 0, fileByte.Length);

}*/

}

catch (Exception es)

{

retvalue = es.Message.ToString();

}

finally

{

Response.Write(retvalue);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值