2017-04-12 35 views
0

我曾在網絡應用程序中導出數據,我想用C#,Javascript中的另存爲對話框保存它們。如何在asp.net web應用程序中使用按鈕進行顯示另存爲對話框點擊

function openDialog() { 

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

測試

+0

如果你想要顯示保存爲彈出窗口,你將不得不從服務器推送文件。在瀏覽器中生成的文件不會顯示此彈出窗口。現在在您的操作方法中,只需返回一個文件即可。當有人瀏覽該方法時,他會得到彈出窗口。 –

回答

0
[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; 
    } 

在UI

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

當鏈路用戶點擊,另存爲對話框將會出現。

相關問題