ASP.Net传输文件

发布于 2024-12-10 12:35:47 字数 754 浏览 0 评论 0原文

我正在 ASP.net 中编写一个 Web 应用程序。

我正在尝试显示一个文件对话框,以便从服务器下载某些内容。

我将适当的文件数据存储在名为 file 的变量中。

文件有字段: FileType - 文件的 MIMEType
FilePath - 服务器端文件路径

这是到目前为止的代码:

Response.Clear();
Response.ContentType = file.FileType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" +     GetFileName(file));
Response.TransmitFile(file.FilePath) ;
Response.End(); 

GetFileName 是一个从附件对象获取文件名的函数。我只存储路径。

上面的代码位于名为“Download_Clicked”的函数中,该函数是单击时触发的事件。该事件被映射到 LinkBut​​ton。

问题是当我运行上面的代码时,没有任何反应。不出现标准对话框。

我尝试了标准的故障排除,例如确保文件存在并确保路径正确。他们都已经准确无误了。

我的猜测是,因为我的机器也是服务器,所以它可能无法正常处理。

提前致谢。

编辑1:尝试将控制权放到另一个页面上,工作正常。

编辑 2:通过从 AJAX 更新面板中删除控制解决了问题。

I am writing a webapplication in ASP.net.

I am trying to make a file dialog box appear for downloading something off the server.

I have the appropriate file data stored in a variable called file.

File has fields:
FileType - The MIMEType of the file
FilePath - The server-side file path

Here's the code so far:

Response.Clear();
Response.ContentType = file.FileType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" +     GetFileName(file));
Response.TransmitFile(file.FilePath) ;
Response.End(); 

GetFileName is a function that gets me the filename from an attachment object. I only store the path.

The above code is in a function called "Download_Clicked" that is an event that triggers on click. The event is mapped to a LinkButton.

The problem is that when I run the above code, nothing happens. The standard dialog box does not appear.

I have attempted the standard trouble-shooting such as making sure the file exists, and ensuring the path is correct. They are both dead on the mark.

My guess is that because my machine is also the server, it may not be processing properly somehow.

Thanks in advance.

Edit 1: Attempted putting control onto another page, works fine.

Edit 2: Resolved issue by removing control from AJAX Update Panel.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

感受沵的脚步 2024-12-17 12:35:47

我找到了另一个可以在不删除更新面板的情况下执行此操作的方法。将下面的代码放入页面加载中,您现在就可以使用该按钮触发下载。

ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(Button);

I've found another to do this without removing the update panel. Place the code below in your page load and you'll now be able to use that button to trigger a download.

ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(Button);

烂柯人 2024-12-17 12:35:47

请改用 Response.WriteFile() 。

另外,不要使用 Response.End()!这会中止线程。使用 Response.Flush(); Response.Close();

Use Response.WriteFile() instead.

Also, don't use Response.End()! This aborts the thread. Use Response.Flush(); Response.Close();

瞎闹 2024-12-17 12:35:47

尝试更改

Response.AppendHeader("Content-Disposition", "attachment; filename=" +     GetFileName(file));

Response.AppendHeader("Content-Disposition", "attachment; filename=" +     Path.GetFileName(GetFileName(file))));

如果这不起作用,您可以随时使用 Response.BinaryWrite 或 Resonse.Write 将文件流式传输到 Web 浏览器
以下是如何使用 Response.Write 或 Response.BinaryWrite 传输文件。将这些函数放在库中的某个地方,然后根据需要调用它们

 public void SendFileToBrowser(String FileName, String MIMEType, String FileData)
    {
        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName);
        Response.ContentType = MIMEType;
        Response.Buffer = true;
        Response.Write(FileData);
        Response.End();
    }

    public void SendFileToBrowser(String FileName, String MIMEType, Byte[] FileData)
    {
        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName);
        Response.ContentType = MIMEType;
        Response.Buffer = true;
        Response.BinaryWrite(FileData);
        Response.End();
    }

然后在某个地方像这样调用这些函数

SendFileToBrowser("FileName.txt", "text/plain", "Don't try this from an Update Panel. MSAjax does not like it when you mess with the response stream.");

Try changing

Response.AppendHeader("Content-Disposition", "attachment; filename=" +     GetFileName(file));

To

Response.AppendHeader("Content-Disposition", "attachment; filename=" +     Path.GetFileName(GetFileName(file))));

If that doesn't work, you can always use Response.BinaryWrite or Resonse.Write to stream the file to the web browser
Here is how transmit the file using Response.Write or Response.BinaryWrite. Put these functions in a library somewhere then call them as needed

 public void SendFileToBrowser(String FileName, String MIMEType, String FileData)
    {
        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName);
        Response.ContentType = MIMEType;
        Response.Buffer = true;
        Response.Write(FileData);
        Response.End();
    }

    public void SendFileToBrowser(String FileName, String MIMEType, Byte[] FileData)
    {
        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName);
        Response.ContentType = MIMEType;
        Response.Buffer = true;
        Response.BinaryWrite(FileData);
        Response.End();
    }

Then somewhere you call these functions like so

SendFileToBrowser("FileName.txt", "text/plain", "Don't try this from an Update Panel. MSAjax does not like it when you mess with the response stream.");
溺渁∝ 2024-12-17 12:35:47

请参阅初始帖子的编辑。

删除了 Ajax 更新面板以解决该错误。专家组正在停止将帖子发回服务器。

有关更多信息,请参阅 Cris Valenzuela 的评论。

See edit on initial post.

Removed Ajax Update Panel to resolve the error. The panel was stopping the post back to the server.

For more info, see Cris Valenzuela's comment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文