ASP.NET MVC:C#下载文件并另存为对话框 [英] ASP.NET MVC: C# Download file and save as dialog

查看:1234
本文介绍了ASP.NET MVC:C#下载文件并另存为对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了这个code这将产生一个Excel US preadsheet并将其保存到指定位置。我想然后从存储的位置读取文件,然后询问用户,然后他们想去的地方存放它显示一个另存为对话框。

  Excel.Application excelApp = NULL;
            Excel.Workbook WB = NULL;
            Excel.Worksheet WS = NULL;
            Excel.Range范围= NULL;excelApp =新Excel.Application();
            WB = excelApp.Workbooks.Add();
            WS = wb.Worksheets.get_Item(1),为Excel.Worksheet;对(INT I = 0; I&小于10 ++ⅰ){
    ws.Cells [1,1] = I +
}wb.SaveAs(@C:\\ TEST.XLS,Excel.XlFileFormat.xlWorkbookNormal);
wb.Close(真);
excelApp.Quit();

如何按以下格式下载?

 字符串str =你好,世界;字节[]字节= System.Text.Encoding.UTF8.GetBytes(STR);返回文件(字节,text / plain的);


解决方案

由于是一个Excel文档保存在 C:\\ TEMP \\ excelDoc.xls 并考虑到你有有一个WebForm像这样的链接

 < ASP:LinkBut​​ton的=服务器ID =GetExcel的OnClick =GetExcel_Click>下载< / ASP:LinkBut​​ton的>

在您的code,后面你可以从磁盘中读取文件,并通过这样的事情发送到用户

 保护无效GetExcel_Click(对象发件人,EventArgs的发送)
    {
        变种文件名=excelDoc.xls;
        使用(VAR CS =新的FileStream(@C:\\ TEMP \\+文件名,FileMode.Open))
        {
            Response.Clear();
            Response.AddHeader(内容处置,附件;文件名=+文件名);
            Response.ContentType =应用程序/ vnd.ms-EXCEL;
            字节[]缓冲区=新的字节[32768]
            INT读;
            而((读取= cs.Read(缓冲液,0,buffer.Length))大于0)
            {
                Response.OutputStream.Write(缓冲,0,读);
            }            到Response.End();
            Response.Flush();
        }
    }

i've written this code which will generate an excel spreadsheet and save it to a specified location. I want to then display a "Save as" dialogue box by reading the file from the stored location and then asking then user where they want to store it.

Excel.Application excelApp = null;
            Excel.Workbook wb = null;
            Excel.Worksheet ws = null;
            Excel.Range range = null;

excelApp = new Excel.Application();
            wb = excelApp.Workbooks.Add();
            ws = wb.Worksheets.get_Item(1) as Excel.Worksheet;

for(int i = 0; i< 10;++i) {
    ws.Cells[i, 1] = i+
}

wb.SaveAs(@"C:\test.xls", Excel.XlFileFormat.xlWorkbookNormal);
wb.Close(true);
excelApp.Quit();

How to download in the following format?

string str = "Hello, world"; 

byte[] bytes = System.Text.Encoding.UTF8.GetBytes(str); 

return File(bytes, "text/plain"); 

解决方案

Given that is an Excel doc saved at c:\temp\excelDoc.xls and given that you have a WebForm that has a link like this

<asp:LinkButton runat="server" ID="GetExcel" OnClick="GetExcel_Click">Download</asp:LinkButton>

In your code-behind you can read the file from disk and send to the user by something like this

    protected void GetExcel_Click(object sender, EventArgs e)
    {
        var fileName = "excelDoc.xls";
        using (var cs = new FileStream(@"c:\temp\" + fileName, FileMode.Open))
        {
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
            Response.ContentType = "application/vnd.ms-excel";


            byte[] buffer = new byte[32768];
            int read;
            while ((read = cs.Read(buffer, 0, buffer.Length)) > 0)
            {
                Response.OutputStream.Write(buffer, 0, read);
            }

            Response.End();
            Response.Flush();
        }
    }

这篇关于ASP.NET MVC:C#下载文件并另存为对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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