表单提交文件
代码
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
<script type="text/javascript">
function upfile() {
var form = document.dform;
// check the filename first
var file = document.getElementById("ifile").value;
var pos = file.lastIndexOf('.');
var ext = file.substring(pos + 1).toLowerCase();
if (ext != 'jpg' && ext != 'gif' && ext != 'png')
{
alert('错误:您选择上传必须是 jpg/gif/png 文件!');
return;
}
// replace form options
var action = form.action;
var enctype = form.enctype;
var target = form.target;
form.action ="default3.aspx";
form.enctype = 'multipart/form-data';
if (typeof form.encoding != 'undefined') form.encoding = 'multipart/form-data';
form.submit();
}
</script>
</head>
<body>
<form id="dform" name="dform" method="post" runat="Server">
<div>
<input id="ifile" name="ifile" type="file" onchange="upfile()" />
</div>
</form>
</body>
</html>
<head runat="server">
<title>无标题页</title>
<script type="text/javascript">
function upfile() {
var form = document.dform;
// check the filename first
var file = document.getElementById("ifile").value;
var pos = file.lastIndexOf('.');
var ext = file.substring(pos + 1).toLowerCase();
if (ext != 'jpg' && ext != 'gif' && ext != 'png')
{
alert('错误:您选择上传必须是 jpg/gif/png 文件!');
return;
}
// replace form options
var action = form.action;
var enctype = form.enctype;
var target = form.target;
form.action ="default3.aspx";
form.enctype = 'multipart/form-data';
if (typeof form.encoding != 'undefined') form.encoding = 'multipart/form-data';
form.submit();
}
</script>
</head>
<body>
<form id="dform" name="dform" method="post" runat="Server">
<div>
<input id="ifile" name="ifile" type="file" onchange="upfile()" />
</div>
</form>
</body>
</html>
代码
protected void Page_Load(object sender, EventArgs e)
{
if (Request.Files.Count > 0)
{
string path = Server.MapPath("uploadfile/");
HttpPostedFile postfile = Request.Files[0];
if (postfile.ContentLength > 0)
{
string name = DateTime.Now.ToString("yyMMddHHmmssfff") + postfile.FileName;
postfile.SaveAs(path + name);
Page.ClientScript.RegisterStartupScript(this.GetType(), "js", "<script>printimage('uploadfile/" + name + "')</script>");
}
}
}
{
if (Request.Files.Count > 0)
{
string path = Server.MapPath("uploadfile/");
HttpPostedFile postfile = Request.Files[0];
if (postfile.ContentLength > 0)
{
string name = DateTime.Now.ToString("yyMMddHHmmssfff") + postfile.FileName;
postfile.SaveAs(path + name);
Page.ClientScript.RegisterStartupScript(this.GetType(), "js", "<script>printimage('uploadfile/" + name + "')</script>");
}
}
}