Error : http query string is too long - c#

I use the below code in mvc to download Excel file but it shows error query string too long.
public ActionResult Download(string input)
{
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("Content-Disposition", "attachment; filename= download.xlsx");
Response.AddHeader("Content-Type", "application/Excel");
Response.ContentType = "application/vnd.ms-excel";
Response.WriteFile(input);
Response.End();
return Content(String.Empty);
}

This code works for me for PDF:
public FileStreamResult DownnloadPDF(int id)
{Document document = new Document();
MemoryStream stream = new MemoryStream();
PdfWriter pdfWriter = PdfWriter.GetInstance(document, stream);
pdfWriter.CloseStream = false;
document.Open();
formatPDF(document, model);
document.Close();
stream.Flush(); //Always catches me out
stream.Position = 0; //Not sure if this is required
return File(stream, "application/pdf", "title" + ".pdf");
}
I really think that your ActionResult will not work.

Related

PDFsharp not giving pdf file in response

I am working on export functionality using PDFsharp in .Net MVC I am not getting any error in my code but not getting PDF file in response.
I have tried doing it manually writing it to specific path with the help of : System.IO.File.WriteAllBytes(path1, bytes); and it's working perfectly, but I am not getting PDF in Response with the help of
Response.BinaryWrite(bytes);
Response.OutputStream.Write(bytes, 0, bytes.Length);
Anyone have faced this type of issue or someone from community please help
Here is my code :
public bool ExportPdf(string htmlcontenttbl)
{
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition",
"attachment;filename=Myfile.pdf");
Response.ContentType = "application/pdf";
Response.Charset = "";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
PdfDocument document =
PdfGenerator.GeneratePdf(htmlcontenttbl.ToString(), PdfSharp.PageSize.A4,
30);
var config = new PdfGenerateConfig();
config.PageOrientation = PageOrientation.Landscape;
config.PageSize = PageSize.A4;
config.MarginBottom = 30;
config.MarginTop = 30;
byte[] bytes = null;
using (MemoryStream stream = new MemoryStream())
{
document.Save(stream, true);
bytes = stream.ToArray();
}
var path1 = Server.MapPath("~/Images/" +
DateTime.Now.TimeOfDay.Ticks + "result.pdf");
//System.IO.File.WriteAllBytes(path1, bytes);
//Response.BinaryWrite(bytes);
//Response.OutputStream.Write(bytes, 0, bytes.Length);
Response.Flush();
Response.End();
return true;
}
thank you,

Value cannot be null. Parameter name: elementId in PDFsharp

I am working on Export to PDF funtionality using C# and PDFSharp. I am getting this error:
Value cannot be null.
Parameter name: elementId
The error is on this line :
PdfDocument document = PdfGenerator.GeneratePdf(htmlcontenttbl.ToString(), PdfSharp.PageSize.A4, 30);
Here's the whole method:
public bool ExportPdf(string htmlcontenttbl)
{
Response.ClearContent();
Response.ClearHeaders();
Response.Buffer = true;
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=Myfile.pdf");
//Response.AddHeader("Content-Disposition", "inline;filename=file.pdf");
//Response.AppendHeader("Content-Disposition", "attachment; filename=Myfile.pdf");
Response.Charset = "";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
PdfDocument document = PdfGenerator.GeneratePdf(htmlcontenttbl.ToString(), PdfSharp.PageSize.A4, 30);
var config = new PdfGenerateConfig();
config.PageOrientation = PageOrientation.Landscape;
config.PageSize = PageSize.A4;
config.MarginBottom = 30;
config.MarginTop = 30;
//PdfDocument document = PdfGenerator.GeneratePdf(htmlcontenttbl, config);
byte[] bytes = null;
using (MemoryStream stream = new MemoryStream())
{
document.Save(stream, true);
bytes = stream.ToArray();
}
//var path1 = Server.MapPath("~/Images/" + DateTime.Now.TimeOfDay.Ticks + "result.pdf");
//System.IO.File.WriteAllBytes(path1, bytes);
//Response.TransmitFile(path1, 0, bytes.Length);
//Response.OutputStream.Write(bytes, 0, bytes.Length);
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
return true;
}
That exception can occur if you have a blank anchor tag.
It is likely ultimately being generated from here.
You should remove the blank anchor tag.

Component One Exporting to Excel file

I'm trying export out using C1 excel with a prompt to the user where to save the file.
However I have tried the following codes but it doesn't seems to work.
Response.Clear();
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition","attachment;filename=CategoryReport.xls");
System.IO.MemoryStream ms = new System.IO.MemoryStream();
xbook.Save(ms, C1.C1Excel.FileFormat.Biff8);
ms.WriteTo(Response.OutputStream);
ms.Close();
ms.Dispose();
xbook.Dispose();
Please help =)
You can use an HTTP Handler (DownloadFile.ashx):
public class DownloadFile : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
// retrieve your xbook
System.IO.MemoryStream ms = new System.IO.MemoryStream();
xbook.Save(ms, C1.C1Excel.FileFormat.Biff8);
xbook.Dispose();
ms.Position = 0;
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition","attachment;filename=CategoryReport.xls");
Response.BufferOutput = true;
Response.OutputStream.Write(ms.ToArray(), 0, (int)ms.Length);
response.Flush();
response.End();
}
public bool IsReusable
{
get
{
return false;
}
}
}
In the export event in code behind:
Response.Redirect("YourPathToHttpHandler/DownloadFile.ashx");

Edit pdf itextsharp and send to the browser

I want to edit and existing pdf and to send the result to the broswer
Here is my methode:
public static byte[] Generate()
{
var templatePath = HttpContext.Current.Server.MapPath("~/my_template.pdf");
// Based on:
// http://www.johnnycode.com/blog/2010/03/05/using-a-template-to-programmatically-create-pdfs-with-c-and-itextsharp/
var reader = new PdfReader(templatePath);
var outStream = new MemoryStream();
var stamper = new PdfStamper(reader, outStream);
var form = stamper.AcroFields;
var fieldKeys = form.Fields.Keys;
foreach (string fieldKey in fieldKeys)
{
if (form.GetField(fieldKey) == "MyTemplatesOriginalTextFieldA")
form.SetField(fieldKey, "1234");
if (form.GetField(fieldKey) == "MyTemplatesOriginalTextFieldB")
form.SetField(fieldKey, "5678");
}
// "Flatten" the form so it wont be editable/usable anymore
stamper.FormFlattening = true;
stamper.Close();
reader.Close();
return outStream.ToArray();
}
and
The method call
byte[] buffer = Generate();
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", buffer.Length.ToString());
Response.BinaryWrite(buffer);
Can someone help me in this case ?
Add this line
byte[] buffer = Generate();
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", buffer.Length.ToString());
Response.AppendHeader("Content-Disposition", "attachment; filename=youPDFName.pdf");
Response.BinaryWrite(buffer);
Response.End();
I have added Content-Disposition and response.end in code.
tested above code , work fine. let me know if it works you.

How to convert pdf Byte[] Array to downloadable file using iTextSharp

Hei guys I have this byte array i want to convert to pdf and make it available for download. Anybody has any idea how this is done?
here is my Action Controller
public ActionResult DownloadLabTestResult(string labTestResultID)
{
PdfReader pdfReader = new PdfReader("Xue_Tang.pdf");
MemoryStream stream = new MemoryStream();
PdfStamper stamper = new PdfStamper(pdfReader, stream);
pdfReader.Close();
stamper.Close();
stream.Flush();
stream.Close();
byte[] pdfByte = stream.ToArray();
// So i got the byte array of the original pdf at this point. Now how do i convert this
// byte array to a downloadable pdf? i tried the method below but to no avail.
MemoryStream ms = new MemoryStream(pdfByte);
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=labtest.pdf");
Response.Buffer = true;
Response.Clear();
Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length);
Response.OutputStream.Flush();
Response.End();
return new FileStreamResult(Response.OutputStream, "application/pdf");
}
I am using similar code with a few differences:
Response.Clear();
MemoryStream ms = new MemoryStream(pdfByte);
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=labtest.pdf");
Response.Buffer = true;
ms.WriteTo(Response.OutputStream);
Response.End();
Call Reponse.Clear() earlier.
Use MemoryStream.WriteTo to write to Response.OutputStream.
Edit: sorry, I didn't see that you are using ASP.NET MVC, the above code is in a WebForms aspx page.
For ASP.NET MVC, couldn't you just do
return new FileStreamResult(ms, "application/pdf");
?

Categories