ASP.NET文件上传,为每个用户建立一个上传目录
文件上传界面既可以用Html的input file控件,又可以用FileUpload控件,只要将Html的input file控件加上runat="server",就会发现两者的功能完全是一模一样,上传的代码是共用的,不需要做任何改变。我想微软在将Html控件将成标准控件时应该只是多加了runat="server"吧。放入上述两者的任一控件后,添加一个标准的Button按钮(Html按钮也行,不过需要加上runat="server"),双击Button按钮,产生点击事件。在点击事件中写入以下代码:
首先检查是否已经选了文件
if (this.myFile.PostedFile != null)
{
检查文件根目录是否存在,不存在就要创建
if (!System.IO.Directory.Exists(Server.MapPath("~")+@"\photoes"))
{
System.IO.Directory.CreateDirectory(Server.MapPath("~")+@"\photoes");
}
此处Server.MapPath("~")用来表示项目根目录的物理路径。
接下来创建用户文件夹,根据用户ID创建
if(!System.IO.Directory.Exists(Server.MapPath("~")+@"\photoes\"+userID))
{
System.IO.Directory.CreateDirectory(Server.MapPath("~")+@"\photoes\"+userID)
}
string orignalName = this.myFile2.PostedFile.FileName;//获取客户机上传文件的路径
int lastdotlocation = orignalName.LastIndexOf(".");
string extendName = orignalName.Substring(lastdotlocation);//获取扩展名
if (extendName != ".gif" && extendName != ".jpg" && extendName != ".jpeg" && extendName != ".png")
{
Response.Write("Wrong format");
Response.End();
}//检查文件格式
string newName = DateTime.Now.Millisecond.ToString() + "_" + myFile2.PostedFile.ContentLength.ToString() + extendName;//对文件进行重命名
myFile.PostedFile.SaveAs(Server.MapPath("~") + @"\photoes\" +userID+@"\"+ newName);
}
相关文档:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient; ......
内容页访问MasterPage中的控件,有两种解决方案:
一、是用弱类型访问
使用 FindControl 方法获取模板页的控件
((Label)Master.FindControl("Label1")).Text = "xxx";
二、给模板页添加属性来使用强类型访问(推荐)
模板页定义;
&nb ......
学asp.net不是很久,做了一个 OA 的项目,遇到很多问题,在此Mark一下,这些都是项目中经常遇到的问题,我搜集网上的解决方案,做了一个小的总结.也让遇到同样问题的IT学子有些帮助.
注: 人人为我,我为人人!
1、解决了 framework2.0 架构下 子页 内容 引用updatepanel 导致 ajax控件 警告为:未知元素,代码排版紊 ......
首先,在pageload里写入以下代码:Response.Write("<script>window.opener=null;window.close();</script>");
其次,在head里写下如下JS代码: <script language="JavaScript">
<!--
function openwin() { window.open ("Default.aspx", "newwindow", "height=600, width=600, toolbar=no, menubar=n ......