I am exporting a excel file using the responce object like,
response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";");
It is opening a dialog box (open/save/cancel).But Now I don't need the dialog box.
I want to directly SAVE file without dialog. Can any body help me out with some code link/any?
It is not possible. Sorry, but what do you think if you open web page and this page write some file on you hard drive?
This might solve your problem:
Response.ClearContent();
Response.AddHeader("Content-type", "application/vnd.ms-excel");
Response.AddHeader("content-disposition", "attachment;filename=Export.xls");
Response.ContentType = "application/excel";
var swr = new StringWriter();
var tw = new HtmlTextWriter(swr);
grd.RenderControl(tw);
Response.Write(swr.ToString());
Response.Flush();
Response.End();
tw.Close();
swr.Close();
Related
I am using NPOI to save files to an .xls format.
I would like to save an empty file for now that a user has a dialog box and is prompted to save it locally.
The problem I have is I cannot set the Response fields, because
The name 'Response' does not exist in the current context.
This is the class I want to use:
https://learn.microsoft.com/en-us/dotnet/api/system.web.httpresponse?view=netframework-4.8
//Load all documents sorted by DocNumber and export them
var documents = await _ctx.Db.Documents
.AsNoTracking()
.OrderByDescending(d => d.Number)
.ToListAsync();
var workbook = new HSSFWorkbook();
var sheet = workbook.CreateSheet("Invoicing_Docs");
using (var exportData = new MemoryStream())
{
workbook.Write(exportData);
string saveAsFileName = string.Format("Invoicing_Docs-{0:d}.xls", DateTime.UtcNow).Replace("/", "-");
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", saveAsFileName));
Response.Clear();
Response.BinaryWrite(exportData.GetBuffer());
Response.End();
}
I have installed System.Web in the project.
When I ctrl + . on Response I have 3 choices:
Install package "IdentityMode",
Install package "Selenium.WebDriver"
Install package "Swashbuckle.AspNetCore.Swagger"
Also this is the article, I am following:
https://steemit.com/utopian-io/#haig/how-to-create-excel-spreadsheets-using-npoi
I don't need any of those. What am I doing wrong?
I found out what I need to do.
I was not using the controller I was writing a service that is called in the controller.
All I had to do is inject Response in the Method of the service.
I have written some Asp.Net MVC code that returns an Html file FileResult.
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write(htmlReport);
writer.Flush();
stream.Position = 0;
return File(stream, "text/html", fileName);
Using my local environment the HTML file looks fine when opened but after deploying to the server and downloading the same file some unwanted  characters can be seen. How could the difference in server environments be affecting a file which is downloaded to the users pc?
You need to specify the file encoding. You are most likely using UTF8, but other formats are available.
var writer = new StreamWriter(stream, System.Text.Encoding.UTF8);
Why use the StreamWriter at all?
return Content(htmlReport, "text/html", System.Text.Encoding.UTF8);
Or if you want to be using File as your return result try:
byte[] bArray = System.Text.Encoding.UTF8.GetBytes(htmlReport);
return File(bArray, "text/html", fileName);
I have an MVC controller with an action which is hit simply to return a PDF to the user. After I return the PDF, I would like to display a Thank You page. When I try to do this, however, I get the following message:
"Cannot redirect after HTTP headers have been sent."
Is there any way to do this?
Here is the code that returns the PDF:
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}", outputFile));
Response.ContentType = "application/pdf";
Response.WriteFile(outputFile);
Response.Flush();
Response.Close();
I have created a simple view for the Thank You page. Can I stuff it into the response somehow?
Thanks,
Jay
return a jquery function as content which calls for 2 links. 1 the pdf and the otherone pops up a page. You have to call for the pdf first.
I have my controller action method returning pdf file as
public FileContentResult GetPDF(string filename)
{FileContentResult filecontent= new FileContentResult(Contents, "application/pdf");
HttpContext.Response.AddHeader("Content-Disposition", "inline; filename=" + filename);
return filecontent;
}
here Contents is byte[] type.
If in my Adobe reader Edit->Preferences->Internet unchecked the "Display PDF in the browser" opens file but not with the filename in Adobe Reader. and if I checked that then it is not opening in Browser Tab for this I also tried with opening pdf file from a website it opened it browser tab so no issues with browser.
I need to be able to open pdf in browser tab with file name. can anybody help me because i find no solution after lot of search. Is iframe setting needed?
try this if it works,
return File(filecontent,mimetype,filename);
or use FileStreamResult , idon't what exactly it does but try ,may works
regards,
kris
I have an ASP.NET MVC2 application in development and I am having problems rendering a .pdf file from our production server.
On my Visual Studio 2010 integrated development server everything works fine, but after I publish the application to the production server, it breaks. It does not throw any exceptions or errors of any kind, it simply does not show the file.
Here's my function for displaying the PDF document:
public static void PrintExt(byte[] FileToShow, String TempFileName,
String Extension)
{
String ReportPath = Path.GetTempFileName() + '.' + Extension;
BinaryWriter bwriter =
new BinaryWriter(System.IO.File.Open(ReportPath, FileMode.Create));
bwriter.Write(FileToShow);
bwriter.Close();
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = ReportPath;
p.StartInfo.UseShellExecute = true;
p.Start();
}
My production server is running Windows Server 2008 and IIS 7.
You cannot expect opening the default program associated with PDF file browsing on the server. Try returning the file into the response stream which will effectively open it on the client machine:
public ActionResult ShowPdf()
{
byte[] fileToShow = FetchPdfFile();
return File(fileToShow, "application/pdf", "report.pdf");
}
And now navigate to /somecontroller/showPdf. If you want the PDF opening inside the browser instead of showing the download dialog you may try adding the following to the controller action before returning:
Response.AddHeader("Content-Disposition", "attachment; filename=report.pdf");
i suggest you use ASP.NET MVC FileResult Class to display the PDF.
see http://msdn.microsoft.com/en-us/library/system.web.mvc.fileresult.aspx
your code open`s the PDF on the webserver.
Here's how I did it.
public ActionResult PrintPDF(byte[] FileToShow, String TempFileName, String Extension)
{
String ReportPath = Path.GetTempFileName() + '.' + Extension;
BinaryWriter bwriter = new BinaryWriter(System.IO.File.Open(ReportPath, FileMode.Create));
bwriter.Write(FileToShow);
bwriter.Close();
return base.File(FileToShow, "application/pdf");
}
Thank you all for your efforts. Solution I used is the most similar to the Darin's one (almost the same, but his is prettier :D), so I will accept his solution.
Vote up for all of you folks (both for answers and comments)
Thanks