Re: [問題]請問C#寫的asp程式如何上傳至網頁?
我不太知道你想要的是什麼,不過我這有個範例從書上抄的,你看看有沒有用
<script Language="c#" runat="server">
void btnSend_click(object sender, EventArgs e)
{
if(txtFile.PostedFile.ContentLength == 0)
{
lblShow.Text="檔案傳送失敗!";
}
else
{
lblShow.Text="檔案大小->"+txtFile.PostedFile.ContentLength+"<br>";
lblShow.Text+="檔案類型->"+txtFile.PostedFile.ContentType+"<br>";
lblShow.Text+="檔案名稱->"+txtFile.PostedFile.FileName;
//取出上傳的檔案名稱
string f_name=Get_File_Name(txtFile.PostedFile.FileName);
//將上傳的檔案儲存在伺服器端指定的資料夾下
txtFile.PostedFile.SaveAs(Server.MapPath(".")+"\\"+f_name);
}
}
//Get_File_Name 函式可由完整的檔案路徑中取得檔案名稱
string Get_File_Name(string str)
{
string new_str="";
string file_name="";
for(int i=str.Length-1 ; i>=0 ; i--)
{
if(str[i].ToString() !="\\")
{
new_str += str[i].ToString();
}
else
{
break;
}
}
for(int i=new_str.Length-1 ; i>=0 ; i--)
{
file_name += new_str[i].ToString();
}
return file_name ;
}
</script>
<html>
<head><title>檔案上傳</title></head>
<body>
<form enctype ="multipart/form-data" runat="server">
<p>上傳檔案路徑:<Input Type="file" id="txtFile" runat="server"><p>
<p><asp:button id="btnUpload" runat="server" Text="上傳檔案" OnClick="btnSend_click"/></p>
<asp:Label id="lblShow" runat="server" />
</form>
</body>
</html>
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 203.73.152.26
推
140.119.183.211 03/03, , 1F
140.119.183.211 03/03, 1F
推
220.134.23.117 03/04, , 2F
220.134.23.117 03/04, 2F
討論串 (同標題文章)
完整討論串 (本文為第 3 之 3 篇):
C_Sharp 近期熱門文章
PTT數位生活區 即時熱門文章