ASP.NET上傳檔案方式



上傳檔案方法

bool FileOK = false;

if (FileUpload1.HasFile)
{
//如果副檔名有大寫則將他轉為小寫
string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();

//允許的檔案類型
string[] allowExtensions = { ".zip", ".rar", ".7z", ".doc", ".docx","jpg" };

for (int i = 0; i < allowExtensions.Length; i++)
{

if (fileExtension == allowExtensions[i])
{

FileOK = true;

}

}

}

try
{
if (FileOK)
{
if (_topicid == "")
{
_topicid = ViewState["_topicid"].ToString();
}
FileUpload1.Enabled = false;
Buttonup.Enabled = false;
Buttondel.Enabled = true;
string ExtName = System.IO.Path.GetExtension(this.FileUpload1.PostedFile.FileName).ToLower();
string _FSSvr = System.Web.Configuration.WebConfigurationManager.AppSettings["FSSvr"].ToString();
DirectoryInfo di = new DirectoryInfo(_QTItem + _topicid);
if (!di.Exists) //若無資料夾建立資料夾
{
di.Create();
}
ViewState["S_fillp"] = this.FileUpload1.FileName;
string fillp = this.FileUpload1.FileName;
FileUpload1.SaveAs(di + "//" + fillp);
FileUplListBox.Text = "";
FileUplListBox.Text = FileUpload1.FileName;
}
}
catch (Exception ex)
{
TopInfoComm.TopTraceLog("3", "TopicNewadd.aspx 異常: 使用者" + UESRtopic.SSOUserID + "於問卷上傳附件資訊錯誤" + ex.Message + DateTime.Now);

}
}

張貼留言

0 留言