Using kendo.saveAs to download an excel or csv file from an action method - asp.net

I have a Kendo Grid. I'm using a jquery AJAX call to send data from the Grid to an Action method for processing. Ok fine, no problem there. So after the processing of the data i now want to down either an excel file or csv file (which are basically the same thing). But Jquerys AJAX method doesn't allow Action method to return files like (an excel file). I had figured out another way to do this using
window.location.href = "Controller/Actionmethod/MethodName?" + parameters
But this was a no go as my boss wants it all in one Action Method, not two..
So now introduce this into the success call back of my AJAX call:
kendo.saveAs({
dataURI: data.file,
fileName: "PayrollExport.csv"
});
And here is my code for building the csv file which works fine.
var export = Fundraisers.ProcessPledgeCardUltiproExport().Where(t => FundraiserIDs.Contains(t.FundraiserID)).ToList();
var csv = new StringBuilder();
csv.AppendLine(String.Format("Employee Number, Deduction code, Benefit Status, Coverage Start, Deduction Start Date, Employee Amount"));
foreach (var e in export)
{
var newLine = String.Format("{0},{1},{2},{3},{4:d},{5}", e.EmployeeID, "UW", "A", "", new DateTime(DateTime.Now.Year + 1, 1, 1), e.PledgeAmount);
csv.AppendLine(newLine);
}
var byteArray = Encoding.ASCII.GetBytes(csv.ToString());
var stream = new MemoryStream(byteArray);
Now my question is: How do i return this from the action method.?
All i have as of right now is return Json(new { success = true, file = stream });
An example of what i would need that works for a PDF file is something along the line of:
return Json(new { success = true, message = "Verified!", file = "data:text/html;base64," + Convert.ToBase64String(fileContent) });
Any idea on the what i should put in the return for the posted above code after creating the csv file?
Any help would be much appreciated.

Related

Can't return word document, generated in memory stream, for downloading to user

I've got a problem while resolving one task. The task was: Create an opportunity for users to download a .docx document with pasted data. But I'm stuck at the moment at sending the file as a byte array (taken from a MemoryStream) to the context's response. Here's a sample:
using (MemoryStream stream = new MemoryStream(FileBytes))
{
using (WordprocessingDocument myDoc = WordprocessingDocument.Open(stream, true))
{
MainDocumentPart mainPart = myDoc.MainDocumentPart;
foreach (SdtElement obj in mainPart.Document.Body.Descendants<SdtElement>().ToList())
{
foreach (Text t in obj.Descendants<Text>().ToList())
{
switch (t.Text)
{
//.... here is code that fiiling content control's
}
}
}
myDoc.MainDocumentPart.Document.Save();
myDoc.Close();
}
context.Response.Clear();
context.Response.ClearHeaders();
context.Response.ClearContent();
context.Response.AddHeader("content-disposition", "attachment; filename=\"" + DocName + ".docx\"");
context.Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
context.Response.ContentEncoding = Encoding.GetEncoding("ISO-8859-1");
stream.Seek(0, SeekOrigin.Begin);
context.Response.BinaryWrite(stream.ToArray());
}
context.Response.Flush();
The HTTP handler doesn't return any errors - page refresh and download don't start. The handler is calling from JS. Here is the sample of the JS function:
function save_word_doc(id_btn) {
// ... here is code that gets params from default page data
jQuery.post("DataHandler.ashx?CN=" + vCN + "&CommandName=SaveWord&auctionID=" + oFormRecord.auctionID
+ "&user_login=" + user_login
+ ....
+ "&amount=" + oFormRecord.value.amount
+ "&percent=" + percent);
}
By the way, this sample code is working well in an ASP.Net MVC project, where the handler is calling from a link button. But this must work in a Web Forms project.
Update to pretend questions and incorrect answers: the file is generated and saved correctly to the local machine.
I see a few issues in your code. Firstly, new MemoryStream(FileBytes) creates a non-resizable MemoryStream, which is not what you want in case you are changing the WordprocessingDocument. You would should use new MemoryStream() to create a resizable MemoryStream and copy your FileBytes to that MemoryStream.
Secondly, since you are in a using statement, you don't need the following two lines of code. This is done automatically for you.
myDoc.MainDocumentPart.Document.Save();
myDoc.Close();
Lastly, I am not sure about your ContentEncoding value for the binary data you are sending. You might want to use a tool like Fiddler or Postman to verify what happens.

Decrypt PDF file on client side and view with pdf.js

I'm working on a project that all pdf files are encrypted on Web Server.
With XMLHttpRequest I get content of the encrypted pdf file. Then with JavaScript tools I decrypt the file. After all assign the content of file to a javascript variable as decrypted_file. All this is done at client side.
Here is what i want to do;
pdf.js renders and views pdf file that is located on web server or the same directory base.
How could I handle pdf.js to get content from javascript variable not url as "http//yourdomain.com/first-test.pdf or file as "first-test.pdf"?
Any answers are welcome, thank you.
Assuming that you are using the viewer.html of PDF.js, opening a PDF file from data is as easy as calling PDFViewerApplication.open with the right parameters.
Example: Typed arrays (Uint8Array / ArrayBuffer / ..)
// in viewer.html
var data = new Uint8Array( /* ... data ... */ );
PDFViewerApplication.open(data);
Example: Blob / File objects
// in viewer.html
var data = new Blob([ '%PDF....'] , {type: 'application/pdf'});
var url = URL.createObjectURL(data);
PDFViewerApplication.open(url);
Example: data URL (if supported by browser)
var url = 'data:application/pdf;base64,....';
PDFViewerApplication.open(url);
Example: data URL (any browser)
This consists of two steps: Decoding the base64 data-URL, and then converting the binary string to an Uint8Array.
var url = 'data:application/pdf;base64,....';
var data = url.split(';base64,')[1];
// Decode base64
var binaryString = atob(data);
// Convert binary string to Uint8Array
data = new Uint8Array(binaryString.length);
for (var i = 0, ii = binaryString.length; i < ii; ++i) {
data[i] = binaryString.charCodeAt(i);
}
PDFViewerApplication.open(data);
Example: Using PDF.js in a frame
<iframe src="viewer.html" id="pdfjsframe"></iframe>
<script>
var pdfjsframe = document.getElementById('pdfjsframe');
// At the very least, wait until the frame is ready, e.g via onload.
pdfjsframe.onload = function() {
var data = ... data here or elsewhere ... ;
pdfjsframe.contentWindow.PDFViewerApplication.open(data);
};
</script>

Annotation in pdfclown

I am trying to put a sticky note at some x,y location. For this i am using the pdfclown annotation class in .net.
Below is what is available.
using files = org.pdfclown.files;
public override bool Run()
{
files::File file = new files::File();
Document document = file.Document;
Populate(document);
Serialize(file, false, "Annotations", "inserting annotations");
return true;
}
private void Populate(Document document)
{
Page page = new Page(document);
document.Pages.Add(page);
PrimitiveComposer composer = new PrimitiveComposer(page);
StandardType1Font font = new StandardType1Font(document, StandardType1Font.FamilyEnum.Courier, true, false);
composer.SetFont(font, 12);
annotations::Note note = new annotations::Note(page, new Point(78, 658), "this is my annotation...");
note.IconType = annotations::Note.IconTypeEnum.Help;
note.ModificationDate = new DateTime();
note.IsOpen = true;
composer.Flush();
}
Link for annotation
This is putting a sticky note at 78, 658 cordinates in a blank pdf.
The problem is that i want that sticky note in a particular pdf which has some data. How can i modify it...thanks for the help..
I'm the author of PDF Clown -- this is the right way to insert an annotation like a sticky note into an existing page:
using org.pdfclown.documents;
using annotations = org.pdfclown.documents.interaction.annotations;
using files = org.pdfclown.files;
using System.Drawing;
. . .
// Open the PDF file!
using(files::File file = new files::File(#"C:\mypath\myfile.pdf"))
{
// Get the document (high-level representation of the PDF file)!
Document document = file.Document;
// Get, e.g., the first page of the document!
Page page = document.Pages[0];
// Insert your sticky note into the page!
annotations::Note note = new annotations::Note(page, new Point(78, 658), "this is my annotation...");
note.IconType = annotations::Note.IconTypeEnum.Help;
note.ModificationDate = new DateTime();
note.IsOpen = true;
// Save the PDF file!
file.Save(files::SerializationModeEnum.Incremental);
}
Please consider that there are lots of options about the way you can save your file (to an output (in-memory) stream, to a distinct path, as a compacted file, as an appended file...).
If you look at the 50+ samples accompanying the library's distribution, along with the API documentation, you can discover how expressive and powerful it is. Its architecture strictly adheres to the official Adobe PDF Reference 1.7.
enjoy!

Returning Multiple Files from MVC Action

So I've got an MVC 3 application that has a couple places where a text file gets generated and returned in an action using:
return File(System.Text.Encoding.UTF8.GetBytes(someString),
"text/plain", "Filename.extension");
and this works fabulously. Now i've got a situation where I'm trying to return a pair of files in a similar fashion. On the view, i have an action link like "Click here to get those 2 files" and i'd like both files to be downloaded much like the single file is downloaded in the above code snippet.
How can I achieve this? Been searching around quite a bit and haven't even seen this question posed anywhere...
Building on Yogendra Singh's idea and using DotNetZip:
var outputStream = new MemoryStream();
using (var zip = new ZipFile())
{
zip.AddEntry("file1.txt", "content1");
zip.AddEntry("file2.txt", "content2");
zip.Save(outputStream);
}
outputStream.Position = 0;
return File(outputStream, "application/zip", "filename.zip");
Update 2019/04/10:
As #Alex pointed out, zipping is supported natively since .NET Framework 4.5, from JitBit and others:
using (var memoryStream = new MemoryStream())
{
using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
var file1 = archive.CreateEntry("file1.txt");
using (var streamWriter = new StreamWriter(file1.Open()))
{
streamWriter.Write("content1");
}
var file2 = archive.CreateEntry("file2.txt");
using (var streamWriter = new StreamWriter(file2.Open()))
{
streamWriter.Write("content2");
}
}
return File(memoryStream.ToArray(), "application/zip", "Images.zip")
}
Sorry for bumping an old question but...
Another alternative would be to initiate multiple file downloads using JavaScript, and serve files in two different Action Methods on ASP.NET's side.
You're saying you have a link:
On the view, i have an action link like "Click here to get those 2
files"
So make this link like this:
Click to get 2 files
<script src="download.js"></script>
I'm using download.js script found here but you can find plenty of different other options, see this SO question: starting file download with JavaScript for example
I would advice to create a zip file to include both the files using steps(ALGORITHM):
Create a Zip file and add the desired files into the zip
Return the zip file having all desired files from the action
Java Syntax (Just for understanding)
FileOutputStream fos = new FileOutputStream("downloadFile.zip");
ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(fos));
zos.putNextEntry(new ZipEntry("Filename1.extension"+));
//write data in FileName1.extension
zos.write(contentBuffer1, 0, len);
zos.putNextEntry(new ZipEntry("Filename2.extension"));
//write data in FileName2.extension
zos.write(contentBuffer2, 0, len);
//write other files.....
zos.close();
Once zip file is created, return the newly created zip file to download.
return File("downloadFile.zip");
.DOT Net Equivalent using DotNetZip
var os = new MemoryStream();
using (var zip = new ZipFile())
{
//write the first file into the zip
zip.AddEntry("file1.txt", "content1");
//write the second file into the zip
zip.AddEntry("file2.txt", "content2");
//write other files.....
zip.Save(os);
}
outputStream.Position = 0;
return File(outputStream, "application/zip", "filename.zip");
Hope this helps!
Look at this SO solution: MVC Streaming Zip File
The advantage of this solution is that it streams the file to the client.
I just implemented this solution a couple of days ago and it worked fantastic.

Downloading data posted to server as a file from Flex

This should be trivial, and I'm pretty sure I did it once before.
I'm trying to post data up to a server and have it bounced back to me as a file download, prompting the native browser file download box. I know the server part works just fine becasue I can post from a demo web form, but when I run the following Flex 3 code, I can't even get the request to fire.
var fileRef:FileReference = new FileReference();
private function saveXmlAsFile(event:MouseEvent):void
{
var fileRequest:URLRequest = new URLRequest();
fileRequest.method = URLRequestMethod.POST;
fileRequest.url = "http://foo.com/dataBounce";
var urlVariables:URLVariables = new URLVariables();
urlVariables.content = "Test content to return" ;
// fileRequest.contentType = "application/x-www-form-urlencoded ";
urlVariables.fileName = "test.xml";
fileRef.addEventListener(SecurityEvent.ALL, onSecurityError);
fileRef.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError2);
fileRef.addEventListener(IOErrorEvent.NETWORK_ERROR, onNetworkError);
fileRef.addEventListener(Event.COMPLETE, onComplete);
try
{
fileRef.download(fileRequest, "test.xml");
}catch(error:Error) {
model.logger.error("unable to download file");
}
}
Note, when the call to fileRef.download is called, I can't see any request being made across the network using the traditional Firebug or HTTPWatch browser tools.
EDIT: I should add that this is for < Flash Player 10, so I can't use the newer direct save as file functionality.
Any suggestions? Thanks.
You need to add fileRef.upload to trigger the upload.
Also I would move the download statement to the onComplete so the file isn't requested before it's been uploaded.
Your explanation is pretty clear, but when I look at your code, I'm feel like I'm missing something.
The code looks like you're trying to do half of the upload part and half of the download part.
I think the code you currently have posted would work to trigger a download if you set the .method value to GET. I believe you will also need to include the filename as part of the .url property.
However, to post something and then trigger a download of it, you need two separate operations - the operation to post the data and then an operation to download it, which should probably be called from the upload operation's onComplete handler.
OK, I believe I figured out one of the things that's going on.
When you don't set the URLRequest.data property, it defaults the request method to "GET".
So, the working code looks like, with the data set to the posted URL variables:
private var fileRef:FileReference;
private function saveRawHierarchy(event:MouseEvent):void
{
var fileRequest:URLRequest = new URLRequest();
fileRequest.method = URLRequestMethod.POST;
fileRequest.url = "http://foo/bounceback";
var urlVariables:URLVariables = new URLVariables();
urlVariables.content = "CONTENT HERE";
urlVariables.fileName = "newFileName.xml";
fileRequest.data = urlVariables;
fileRef = new FileReference();
fileRef.addEventListener(Event.COMPLETE, onComplete);
try
{
fileRef.download(fileRequest, "appHierarchies.xml");
}catch(error:Error) {
model.logger.error("unable to download file");
}
}
Not sure what was wrong about the request not being made before, though.

Resources