几经努力,终于实现了文件上传,不使用asp.net组件,而使用传统asp文件模板方式,方法如下:
上传文件界面模板:(upfile.htm)HTML如下:
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>无标题页</title>
- </head>
- <body>
- <table width="100%" border="0" cellpadding="0" cellspacing="0">
- <form action="testup.aspx" method="post" name="form1" enctype="multipart/form-data">
- <tr>
- <td>
- <input name="FileName" id="FileName" type="FILE" class="tx1" size="30">
- <input name="FileUrlID" type="hidden" id="FileUrlID" value="imgpath">
- <input type="submit" name="Submit" value="上传" class="tx1">
- </td>
- </tr>
- </form>
- </table>
- </body>
- </html>
处理页面的asp.net文件(testup.aspx)的testup.aspx.cs代码如下:
- using System;
- using System.Collections;
- using System.Configuration;
- using System.Data;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using diggcms.Content.Templet;
- public partial class admin_testup : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- Template t = new Template();
- t.SetTemplateFile("upfile.htm");
- function.Response(t.Parse());
- if (function.getRequest("FileUrlID") != "")
- {
- upfile();
- }
- }
- /// <summary>
- /// 处理上传文件
- /// </summary>
- protected void upfile()
- {
- string _fileName,_filelx, _fileExtension,_filesize;
- string _savedir,_saveName;
- _savedir = Server.MapPath(@"upfile"); //保存在当前路径的upfile目录下
- HttpPostedFile myfile = Request.Files["FileName"];
- _fileName = myfile.FileName; //获取文件
- _filelx = myfile.ContentType; //文件类型
- _filesize = myfile.ContentLength.ToString(); //文件大小
- _fileExtension = _fileName.Substring(_fileName.LastIndexOf("."), _fileName.Length - _fileName.LastIndexOf(".")); //扩展名
- _saveName = "测试" + _fileExtension; //保存的文件名
- myfile.SaveAs(_savedir + "\\" + _saveName);
- Response.Write("保存成功");
- }
- }
- //OK处理完毕