(转)html中使用表单和input file上传图片
2014-02-11 08:30
387 查看
本文转载自:http://hi.baidu.com/love_1210/item/120e452b42b2a854c38d59eb
客户端代码:
<form name="form1" method="post" enctype="multipart/form- data" action="requestfile/asprece.aspx">//如果file框没有加runat="server",则 form里一定要加上 enctype="multipart/form-data"这样才可以实现上传文件到服务器;使用了server和没有使用
runat="server"是有区别的.使用了runat="server"的form编译后,action必定是指向本身的网页。而没
有加runat="server"的form可以指向一个网页。
<input type="file" name="file1" style="width:160px;" />
<input type="submit" name="Submit" value="添加" />
</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 < 0)
{
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);
}
}
}
客户端代码:
<form name="form1" method="post" enctype="multipart/form- data" action="requestfile/asprece.aspx">//如果file框没有加runat="server",则 form里一定要加上 enctype="multipart/form-data"这样才可以实现上传文件到服务器;使用了server和没有使用
runat="server"是有区别的.使用了runat="server"的form编译后,action必定是指向本身的网页。而没
有加runat="server"的form可以指向一个网页。
<input type="file" name="file1" style="width:160px;" />
<input type="submit" name="Submit" value="添加" />
</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 < 0)
{
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);
}
}
}
相关文章推荐
- 使用DiskFileItemFactory在Action中获得JSP表单内容(用在有图片上传功能的表单中)
- HTML中文件上传时使用的<input type="file">元素的样式自定义
- html中,文件上传时使用的<input type="file">的样式自定义
- 使用DiskFileItemFactory在Action中获得JSP表单内容(用在有图片上传功能的表单中)
- bootstrap 上传图片插件 file-input 的简单使用
- html,图片上传预览,input file获取文件等相关操作
- 使用input file 上传图片并显示
- ASP.NET同时上传多个文件,和不使用HTMLFileInput上传的方法(resource about file upload)
- bootstrap 上传图片插件 file-input 的简单使用
- yii2 之图片上传插件fileinput使用说明
- html input控件type为file上传文件、图片,及保存为原文件
- Bootstrap文件(图片)上传插件File Input进阶使用说明
- 关于PHP HTML <input type="file" name="img"/>上传图片,图片大小,宽高,后缀名。
- 在套用母版页的页面中使用input file上传图片
- 使用input标签 type='file' 上传图片的问题
- ionic—cordova如何一次上传多张图片(类似input file提交表单)
- input[type=file]中使用ajaxSubmit来图片上传
- BootStrap使用file-input插件上传图片的方法
- HTML中上传与读取图片或文件(input file)----在路上(25)
- HTML使用FileReader对象的readAsDataURL()方法实现图片的上传预览效果