[英]download file from server asp.net
我想將文件從服務器下載到本地主機。
我有一個網上的代碼,該代碼應該可以工作但不能正常工作
protected void Button4_Click(object sender, EventArgs e)
{
//To Get the physical Path of the file(test.txt)
string filepath = Server.MapPath("test.txt");
// Create New instance of FileInfo class to get the properties of the file being downloaded
FileInfo myfile = new FileInfo(filepath);
// Checking if file exists
if (myfile.Exists)
{
// Clear the content of the response
Response.ClearContent();
// Add the file name and attachment, which will force the open/cancel/save dialog box to show, to the header
Response.AddHeader("Content-Disposition", "attachment; filename=" + myfile.Name);
// Add the file size into the response header
Response.AddHeader("Content-Length", myfile.Length.ToString());
// Set the ContentType
Response.ContentType = ReturnExtension(myfile.Extension.ToLower());
// Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead)
Response.TransmitFile(myfile.FullName);
// End the response
Response.End();
}
}
private string ReturnExtension(string fileExtension)
{
switch (fileExtension)
{
case ".htm":
case ".html":
case ".log":
return "text/HTML";
case ".txt":
return "text/plain";
case ".doc":
return "application/ms-word";
case ".tiff":
case ".tif":
return "image/tiff";
case ".asf":
return "video/x-ms-asf";
case ".avi":
return "video/avi";
case ".zip":
return "application/zip";
case ".xls":
case ".csv":
return "application/vnd.ms-excel";
case ".gif":
return "image/gif";
case ".jpg":
case "jpeg":
return "image/jpeg";
case ".bmp":
return "image/bmp";
case ".wav":
return "audio/wav";
case ".mp3":
return "audio/mpeg3";
case ".mpg":
case "mpeg":
return "video/mpeg";
case ".rtf":
return "application/rtf";
case ".asp":
return "text/asp";
case ".pdf":
return "application/pdf";
case ".fdf":
return "application/vnd.fdf";
case ".ppt":
return "application/mspowerpoint";
case ".dwg":
return "image/vnd.dwg";
case ".msg":
return "application/msoutlook";
case ".xml":
case ".sdxl":
return "application/xml";
case ".xdp":
return "application/vnd.adobe.xdp+xml";
default:
return "application/octet-stream";
}
}
現在,當單擊按鈕時,應該將文件從服務器下載到本地主機...但是似乎什么也沒有發生...
我在serer的桌面上有test.txt ...保存文件選項也不在客戶端上。
我發布文件並將其放在服務器的inetpub文件夾中,並從客戶端運行GUI。
任何建議...請幫助
這個程序下載一個文件,如果它在inetpub文件夾中..我想從服務器內的任何位置下載...
??
“寫在”按鈕上,單擊要下載文件的位置
protected void Button1_Click(object sender, EventArgs e)
{
string allowedExtensions = ".mp4,.pdf,.m4v,.gif,.jpg,.png,.swf,.css,.htm,.html,.txt";
// edit this list to allow file types - do not allow sensitive file types like .cs or .config
string fileName = "Images/apple.jpg";
string filePath = "";
//if (Request.QueryString["file"] != null) fileName = Request.QueryString["file"].ToString();
//if (Request.QueryString["path"] != null) filePath = Request.QueryString["path"].ToString();
if (fileName != "" && fileName.IndexOf(".") > 0)
{
bool extensionAllowed = false;
// get file extension
string fileExtension = fileName.Substring(fileName.LastIndexOf('.'), fileName.Length - fileName.LastIndexOf('.'));
// check that we are allowed to download this file extension
string[] extensions = allowedExtensions.Split(',');
for (int a = 0; a < extensions.Length; a++)
{
if (extensions[a] == fileExtension)
{
extensionAllowed = true;
break;
}
}
if (extensionAllowed)
{
// check to see that the file exists
if (File.Exists(Server.MapPath(filePath + '/' + fileName)))
{
// for iphones and ipads, this script can cause problems - especially when trying to view videos, so we will redirect to file if on iphone/ipad
// if (Request.UserAgent.ToLower().Contains("iphone") || Request.UserAgent.ToLower().Contains("ipad")) { Response.Redirect(filePath + '/' + fileName); }
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
Response.WriteFile(Server.MapPath(filePath + '/' + fileName));
Response.End();
}
else
{
litMessage.Text = "File could not be found";
}
}
else
{
litMessage.Text = "File extension is not allowed";
}
}
else
{
litMessage.Text = "Error - no file to download";
}
}
您提到test.txt在服務器的桌面上。 它也位於您要測試的頁面旁邊嗎? 嘗試完全限定到桌面的路徑(“ C:\\ Documents and Settings \\ JohnDoe \\ Desktop \\ test.txt”),或將文件復制到.aspx頁旁邊。
您可以將文件夾更改到您的位置:
Directory.SetCurrentDirectory(HttpContext.Current.Server.MapPath("~ your path in here")
好的,所以我把一個Response.Write之后
string filepath = Server.MapPath("test.txt");
並發現文件路徑指向inetpub文件夾...因此,當我將test.txt放入該文件夾時,它起作用了...所以程序正確。
但是現在如果文件在服務器中的任何其他位置,我應該如何修改程序...我正在使用它,但是歡迎任何建議
好吧,我也能找到任何答案
我刪除了server.mappath並放置了完整位置。.之前不知道為什么會出現問題..但現在它可以工作了...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.