【问题标题】:How to Make show Save As Dialog box in asp.net web Application using Button click如何使用按钮单击在 asp.net Web 应用程序中显示另存为对话框
【发布时间】:2017-04-12 05:33:03
【问题描述】:

我曾在 Web 应用程序中工作,在网格中导出数据,我想使用 C#、Javascript 中的“另存为”对话框保存它们。

function openDialog() {

        var input = $(document.createElement('input'));
        input.attr("type", "file")
        input.trigger('click');
        return false;
    }

测试

【问题讨论】:

  • 如果你想显示保存为弹出窗口,你必须从服务器推送文件。在浏览器中生成的文件不会弹出此弹出窗口。现在在您的操作方法中,只需返回一个文件。当有人浏览该方法时,他会得到弹出窗口。

标签: javascript c# asp.net asp.net-mvc-4


【解决方案1】:
    [HttpGet]
    public async Task<HttpResponseMessage> Get(string fileHash)
    {
        //read file 
        byte[] fileData = await _deviceFileApiService.GetFileData(fileHash);

        HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK)
        {
            Content = new ByteArrayContent(fileData)
        };
        response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
        response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
        {
            FileName = fileResource.FileName
        };

        return response;
    }

在用户界面上

<a href="<you url to action here>/get">save as</a>

当用户点击链接时,将出现另存为对话框。

【讨论】:

    猜你喜欢
    • 2013-04-10
    • 2016-01-03
    • 2012-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-07
    • 2012-08-25
    相关资源
    最近更新 更多