📄 uploadfile.aspx.cs
字号:
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.IO; //注意,导入
public partial class PagesExtMail_AspNet_UploadFile : System.Web.UI.Page
{
log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
protected void Page_Load(object sender, EventArgs e)
{
//我自己有日志,如果你没有去掉就可以.
log.Info("Request.Files.Count=" + Request.Files.Count);
string jSONString = string.Empty;
try
{
if (Request.Files.Count > 0)
{
HttpPostedFile postedFile = Request.Files[0];
string savePath ;
//请在你的 d 盘下要建立一个 upload 这样的测试目录.如果没有,会出错的
//可不要告诉我你连 D 盘都没有,如果这样,算你狠...
savePath = @"D:/upload/";
savePath += Path.GetFileName(postedFile.FileName);
//检查是否在服务器上已经存在用户上传的同名文件
if (File.Exists(savePath))
{
File.Delete(savePath);
}
log.Debug(savePath);
postedFile.SaveAs(savePath);
jSONString = "{success:true,message:'上传成功'}";
}
else
log.Info("Request.Files.Count=" + Request.Files.Count);
}//try
catch (Exception ex)
{
jSONString = "{success:false,message:'上传失败,可能因为上传文件过大导致!'}";
log.Info("上传文件出错:"+ex.Message);
log.Info("堆栈信息是 :" + ex.StackTrace);
}//catch
finally
{
log.Debug("jSONString=" + jSONString);
}
//输出上传文件后的后台相应信息,
//在这里请你注意删除你对应页面的所有 html 脚本,只需要保留 page 头就可以
Response.Write(jSONString);
Response.Flush();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -