test.aspx<form id="Form1" method="post" encType="multipart/form-data" runat="server">
<INPUT id="UP_FILE" style="WIDTH: 336px; HEIGHT: 22px" type="file" size="36" name="UP_FILE">
<asp:button id="Button2" runat="server" Text="上传"></asp:button>請問cs文件里該怎麼寫能完成把選擇的文件或者圖片上傳倒d:\upload里
<INPUT id="UP_FILE" style="WIDTH: 336px; HEIGHT: 22px" type="file" size="36" name="UP_FILE">
<asp:button id="Button2" runat="server" Text="上传"></asp:button>請問cs文件里該怎麼寫能完成把選擇的文件或者圖片上傳倒d:\upload里
http://www.dezai.cn/index/Article_Print.asp?ArticleID=3292
<INPUT id="UP_FILE" style="WIDTH: 336px; HEIGHT: 22px" type="file" size="36" name="UP_FILE" runat=server>增加标记RUNATC#:onclick中写:
UP_FILE.postedfile.save(..SERVER物理路径..)
即可。
{
if (UP_FILE.PostedFile != null)
{
string image_path = @"D:\upload"; if(UP_FILE.PostedFile.FileName == "")
{
ShowMessage("请选择要上传的附件!");
return;
} string file_name = "file"; try
{
UP_FILE.PostedFile.SaveAs(image_path + "\\" + file_name);
}
catch (Exception exc)
{
ShowMessage(exc.ToString());
return;
} }
}
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace CommonFunction
{
/// <summary>
/// upFile 的摘要说明。
/// </summary>
public class upFile : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputFile fileUp;
protected System.Web.UI.WebControls.Label strState;
protected System.Web.UI.WebControls.Button btnUpFile;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btnUpFile.Click += new System.EventHandler(this.btnUpFile_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void btnUpFile_Click(object sender, System.EventArgs e)
{
string filePath="",fileExtName="",mFileName,mPath;
System.Text.StringBuilder strMsg = new System.Text.StringBuilder("上传的文件信息:<hr color=red>");
if("" != fileUp.PostedFile.FileName)
{
filePath =fileUp.PostedFile.FileName;//取得文件路径
fileExtName= filePath.Substring(filePath.LastIndexOf(".")+1); try
{
//取得与 Web 服务器上的指定虚拟路径相对应的物理文件路径。
mPath=Server.MapPath("upfile/");
mFileName=filePath.Substring(filePath.LastIndexOf("\\")+1);;//取得文件名
strMsg.Append("上传的文件类型:" + fileUp.PostedFile.ContentType.ToString() + "<br>");
strMsg.Append("客户端文件地址:" + fileUp.PostedFile.FileName + "<br>");
strMsg.Append("上传文件的文件名:" + mFileName + "<br>");
strMsg.Append("上传文件的扩展名:" + fileExtName);
//保存上传文件到指定的目录
fileUp.PostedFile.SaveAs(mPath + mFileName);
strState.Text = strMsg.ToString();
}
catch(Exception error)
{
Response.Write(error.ToString());
} }
}
}
}
前台的<input type="file" id="upPhoto" name="upPhoto" runat="server" />
自动截取文件名,还有检查扩展名检查大小生成随机文件名
public string ExcuteUpLoad(HtmlInputFile upPhoto,string remote)
{
try
{
if(upPhoto.PostedFile.FileName!=string.Empty)
{
string oldpath = upPhoto.PostedFile.FileName;
string fileName = oldpath.Substring(oldpath.LastIndexOf(@"\")+1);
string remotePath = remote;
string fileExtension = oldpath.Substring(oldpath.LastIndexOf("."));
string newName = GetUniqueString()+fileExtension;
string savedpath = remotePath+newName;if(!checkExt(fileExtension))
{
return string.Empty;
}
if(checkSize(upPhoto.PostedFile.ContentLength))
{
return string.Empty;
}
upPhoto.PostedFile.SaveAs(savedpath);
return newName;
}
}
catch(Exception ee)
{
throw(ee);
}
return string.Empty;
}
private bool checkExt(string FileExt)
{
string [] arrExt = {".jpg",".jpeg",".gif",".png"};
for(int i=0;i<arrExt.Length;i++)
{
if(FileExt.Equals(arrExt[i]))
return true;
}
return false;
}
private bool checkSize(int fileLength)
{
int i = 200;
if(fileLength>i*1024)
return true;
return false;
}
public string GetUniqueString()
{
return DateTime.Now.ToString("yyyymmddhhmmss");
}
{
FileUpModem mod=new FileUpModem();
string filePath="",fileExtName="",mFileName,mPath,sPath;
if("" != fileUp.PostedFile.FileName)
{
filePath =fileUp.PostedFile.FileName;//取得文件路径
fileExtName= filePath.Substring(filePath.LastIndexOf(".")+1);
try
{
//取得与 Web 服务器上的指定虚拟路径相对应的物理文件路径。
sPath=HttpContext.Current.Server.MapPath("upload/");
mPath=(path=="0")? sPath :sPath + path + @"\";
mFileName=filePath.Substring(filePath.LastIndexOf("\\")+1);//取得文件名
//设置文件模型
mod.FileType=mFileName.Substring(mFileName.LastIndexOf("."));
mod.Size=Convert.ToString(fileUp.PostedFile.ContentLength);
mod.Name=mFileName.Substring(0,mFileName.LastIndexOf(".")) + api.StrApi.DateTimeFormat() + mod.FileType;
//保存上传文件到指定的目录
//levin.api.WebResponse.alert( mPath + mFileName);
// if (!IsAllowedExtension(mod.FileType)) api.WebResponse.alertGoBack(Const.Mess_FileTypeNoAllow);
// if (!IsAllowedLength(mod.Size)) api.WebResponse.alertGoBack(Const.Mess_FileLengthNoAllow); fileUp.PostedFile.SaveAs(mPath + mod.Name );
//设置文件模型
mod.PrevName=mFileName;
mod.AbsolutePath ="/upload/" + path + @"/" + mod.Name;
mod.CurrentPath = mPath + mFileName;
//mod.HttpPath=Files.FileApi.AbsPath(mod.Name);
HttpContext.Current.Response.Write(mod.AbsolutePath);
//levin.api.WebResponse.alert(Const.Mess_UploadSuccess);
}
catch
{
//levin.api.WebResponse.alert(Const.Mess_UploadFalse);
}
}
return mod;
}
{
private string strSize;
private string strName;
private string strPrevName;
private string strAbsolutePath;
private string strCurrentPath;
private string strFileType;
private string strHttpPath;
public string HttpPath
{
get {return strHttpPath;}
set {strHttpPath=value;}
}
public string FileType
{
get {return strFileType;}
set {strFileType=value;}
}
public string Size
{
get {return strSize;}
set {strSize=value;}
}
public string Name
{
get {return strName;}
set {strName=value;}
}
public string PrevName
{
get {return strPrevName;}
set {strPrevName=value;}
}
/// <summary>
/// 相对路径
/// </summary>
public string AbsolutePath
{
get {return strAbsolutePath;}
set {strAbsolutePath=value;}
}
public string CurrentPath
{
get {return strCurrentPath;}
set {strCurrentPath=value;}
} }
}
{
protected HtmlInputFile UP_FILE;
}
private void Button2_Click(object sender, System.EventArgs e)
{
string newFileName = DateTime.Now.ToString("yyyyMMddHHmmssffff");
string mPath=Server.MapPath("upload/");
UP_FILE.PostedFile.SaveAs(mPath + "\\" + newFileName);
}
我現在就測試把文件複製到指定的文件夾里..
還是出錯..我是哪里設置有問題嗎
這一句系統沒有自動生成..是我自己加上去的..
string mPath=Server.MapPath("upload\");