?? 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 + -