C#从网络驱动器下载文件

时间:2017-09-11 05:44:09

标签: c# asp.net download

我有一个位于网络驱动器的文件。已创建用户访问权限以具有对路径的完全访问权限。但是,当我运行以下代码来获取文件时,浏览器似乎没有响应。

dt <- read.table(text = "'Date Time' 'Object Name' 'Object Value'
'7/28/2017 8:00' a 465
                 '7/28/2017 7:50' a 465
                 '7/28/2017 7:40' a 464.75
                 '7/28/2017 7:30' a 464.75
                 '7/28/2017 7:20' a 464.75
                 '7/28/2017 7:10' a 465
                 '7/28/2017 7:00' a 465",
                 header = TRUE, stringsAsFactors = FALSE)

我尝试使用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(); } ,但也无效。页面似乎在this.Page.Response.TransmitFile(file.FullName);

之后停止运行

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

无论文件存储在哪里。您的操作必须返回一个文件作为结果:

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) - 必须能够访问该文件。

答案 1 :(得分:0)

我能够通过先将文件从网络驱动器复制到本地路径然后从那里执行TransmitFile来解决这个问题:

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();
}