ASP.net下载并保存用户文件夹文件 [英] ASP.net download and save file in user folder

查看:131
本文介绍了ASP.net下载并保存用户文件夹文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将文件保存到用户的电脑,但我不能做到这一点。
因为正试图保存在C文件中的某个地方这种方法可以让错误。我的理解是不是approriate。我想将文件保存在下载文件夹或提示有一个文件对话框中的用户。

I am trying to save a file to the user pc but I am not able to do it. This method gives error since is trying to save the file in C somewhere. I understand it is not approriate. I would like to save the file in the download folder or prompt the user with a file dialog.

void SaveReport(Telerik.Reporting.Report report, string fileName)
{
    ReportProcessor reportProcessor = new ReportProcessor();
    Telerik.Reporting.InstanceReportSource instanceReportSource = new Telerik.Reporting.InstanceReportSource();
    instanceReportSource.ReportDocument = report;
    RenderingResult result = reportProcessor.RenderReport("PDF", instanceReportSource, null);

    using (FileStream fs = new FileStream(fileName, FileMode.Create))
    {
        fs.Write(result.DocumentBytes, 0, result.DocumentBytes.Length);
    }
}

我怎样才能将文件保存在用户计算机的下载文件夹或提示的FileDialog允许用户选择目的地?

How can I save the file in the download folder of the user machine or prompt a filedialog to allow the user to choose the destination?

推荐答案

您不能强制文件下载到的地方。

You can't force the place the file is downloaded to.

在您的code,你不应该写入文件,但对<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.htt$p$psponse.outputstream%28v=vs.110%29.aspx\"相对=nofollow> 的OutputStream 从响应。

In your code, you shouldn't write to file, but to the OutputStream from the response.

RenderingResult result = reportProcessor.RenderReport("PDF", instanceReportSource, null);

HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=\"file.pdf\"");
HttpContext.Current.Response.OutputStream.Write(result.DocumentBytes, 0, result.DocumentBytes.Length);

这篇关于ASP.net下载并保存用户文件夹文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆