An Issue with PDF creation
Hello, we have a website where we have document upload doc/jpeg/pdf. We can successfully upload it to our DB and then we can view them in the Admin panel. We do however need to create a document download from the binary data that is in the Database. We are using MS SQL and the column containing the data is of "image" type. We use a store procedure to get the information from the data field into a byte array and for all the files everything is working fine except for the PDFs. We are using MVC and C#. Can you give a tip how to save the file?
So far we have tried in the controller:
return File(fileData.Data, System.Net.Mime.MediaTypeNames.Application.Pdf, fileData.Title);
And the fileData.Data is the byte array.
Thank you
Hi Gergana,
"file.Data.Title" most probably have only the title but not the file extension which should be ".pdf". So the code might be as follows:
return
File(fileData.Data, System.Net.Mime.MediaTypeNames.Application.Pdf, fileData.Title +
".pdf"
);
public
ActionResult DownloadPDF()
var libraryManager =
new
LibrariesManager();
var doc = libraryManager.GetDocuments().FirstOrDefault();
var strm = libraryManager.Download(doc);
byte
[] byteArray = ConvertToBytes(strm);
return
File(byteArray, System.Net.Mime.MediaTypeNames.Application.Pdf, doc.Title +
".pdf"
);
public
static
byte
[] ConvertToBytes(Stream stream)
using
(MemoryStream ms =
new
MemoryStream())
stream.CopyTo(ms);
return
ms.ToArray();
@Html.ActionLink(
"Download PDF"
,
"DownloadPDF"
,
"MyWidget1"
,
new
action =
""
)
It did have the extension for the pdf in the title - the value is "test.pdf".
And didn't solve the problem.
Hi Gergana,
An update has been provided to your support ticket, please check and provide more details to that support ticket (code for the controller and view and also model if related, also if you could send the pdf file you have uploaded and the corrupted file after downloaded) and once the case is resolved then share it with the community.
Thanks.
Regards,
Arnob Makhlaqur
Telerik