[英]C# Download File from a network drive
I have a file that is located at a network drive. 我有一个位于网络驱动器上的文件。 The user access is already created to have full access to the path.
用户访问权限已创建,可以完全访问路径。 But it seems that when I ran the following code to get the file, the browser just does not respond.
但是似乎当我运行以下代码来获取文件时,浏览器只是不响应。
FileInfo file = new FileInfo(GetDocumentUploadFolder(ID) + fileName);
// Checking if file exists
if (file.Exists)
{
// Clear the content of the response
this.Page.Response.ClearContent();
// Clear the header of the response
this.Page.Response.ClearHeaders();
// Set the ContentType
this.Page.Response.ContentType = "application/pdf";
// Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead)
this.Page.Response.WriteFile(file.FullName);
// End the response
this.Page.Response.End();
}
I tried using this.Page.Response.TransmitFile(file.FullName);
我尝试使用
this.Page.Response.TransmitFile(file.FullName);
and it also does not work. 而且它也不起作用。 The page seems to stop functioning after
this.Page.Response.End();
在
this.Page.Response.End();
之后,页面似乎停止运行this.Page.Response.End();
Any ideas? 有任何想法吗?
No matter where file a stored. 无论文件存储在哪里。 Your action must return a file as the result:
您的操作必须返回一个文件作为结果:
public FileResult GetBytes()
{
string path = Server.MapPath("~/Files/PDFIcon.pdf");
byte[] mas = System.IO.File.ReadAllBytes(path);
string file_type = "application/pdf";
string file_name = "PDFIcon.pdf";
return File(mas, file_type, file_name);
}
Server.MapPath(filePath string) - must have access to the file. Server.MapPath(filePath string)-必须有权访问该文件。
I am able to do a workaround by copying the files first from the network drive to local path and then do a TransmitFile from there: 我可以通过以下方法来解决该问题:首先将文件从网络驱动器复制到本地路径,然后从那里复制文件:
FileInfo file = new FileInfo(GetDocumentUploadFolder(ID) + fileName);
string strFolder = Server.MapPath(LocalLocation);
string strDestination = Server.MapPath(LocalLocation + "\\" + fileName);
// Checking if file exists
if (file.Exists)
{
if (!Directory.Exists(strFolder))
Directory.CreateDirectory(strFolder);
// Delete contents in this folder
Common.DeleteFiles(strFolder, "*.*");
file.CopyTo(strDestination, true);
// Clear the content of the response
this.Page.Response.ClearContent();
// Clear the header of the response
this.Page.Response.ClearHeaders();
// Set the ContentType
this.Page.Response.ContentType = "application/pdf";
// Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead)
this.Page.Response.TransmitFile(strDestination);
// End the response
this.Page.Response.End();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.