ASP.NE 上传文件控件
protected void Button1_Click(object sender, EventArgs e) { //if (Request["id"]==null && Request["id"]=="") //{ // Response.Write(Request["id"]); //} //return; if (this.FileUpload1.HasFile) //判断控件是否有值 { string fileNameA = Path.GetExtension(this.FileUpload1.FileName); // .bmp //-- string fileNameNo = Path.GetFileName(FileUpload1.PostedFile.FileName); //获取文件名和扩展名 //2017-11-24_110128.bmp string DirectoryName = Path.GetDirectoryName(FileUpload1.PostedFile.FileName); //获取文件所在目录 //"" string Extension = Path.GetExtension(FileUpload1.PostedFile.FileName); //获取扩展名 //.bmp string fileName = Path.GetFileNameWithoutExtension(FileUpload1.PostedFile.FileName); //获取文件名(不包括扩展名) //2017-11-24_110128 string fullPath = Path.GetFullPath(FileUpload1.PostedFile.FileName); //获取文件的绝对路径 //C:\Program Files (x86)\IIS Express\2017-11-24_110128.bmp string PathRoot = Path.GetPathRoot(FileUpload1.PostedFile.FileName); //获取文件所在地分区 //"" //-- string path = Server.MapPath("upload/"); //C:\Users\Administrator\Desktop\后台练习\后台练习\sys\upload\ //在上传的同时重命名 this.FileUpload1.SaveAs(path+Guid.NewGuid()+fileName); //另存为文件 Response.Write("上传成功!"); } else { Response.Write("请选择文件"); } }
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="上传" />
</div>