This is a migrated thread and some comments may be shown as answers.

[Solved] Custom column command resulting in file download

5 Answers 148 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Sam
Top achievements
Rank 1
Sam asked on 29 Feb 2012, 10:46 PM
Hi,

I have a grid which uses a custom column command to send an AJAX request to the server, which I then want to return a file that the browser will prompt the user to download.

I have this:
public ActionResult GetDocument(String path)
{
    var documentStream = _customContext.GetFile(path);
    var mimeType = "application/octet-stream";
 
    return File(documentStream, mimeType, "TestDocument.txt");
}

But this causes a script error in the grid. Does anyone know how I can do what I'm trying to accomplish?

Thanks for any help.

5 Answers, 1 is accepted

Sort by
0
Sam
Top achievements
Rank 1
answered on 01 Mar 2012, 05:21 PM
Any help is really appreciated...
0
Dadv
Top achievements
Rank 1
answered on 01 Mar 2012, 05:21 PM
I don't think you could do that in Ajax mode, but if the result is a stream, using server side could fix that.

 columns.Command(commands => commands
                   
.Custom("Download")
                    .Text("Download")
                   
.DataRouteValues(route => route.Add(o => o.Url).RouteKey("Url"))
                   
.Action("GetDocument", "Grid")).Width(150);
0
Sam
Top achievements
Rank 1
answered on 01 Mar 2012, 07:31 PM
I need this to work through AJAX.

If the grid can not support it directly, I can alternatively send the contents of the file back as a byte array, and then manipulate that in a Javascript function. The issue with this is that I don't know how / or if it's possible to initiate a file download using the contents of a Javascript array.

Any suggestions/ideas?
0
Dadv
Top achievements
Rank 1
answered on 02 Mar 2012, 09:33 AM
it was misunderstood, you could have your grid in ajax mode (select, edit, etc..) but you didn't need to have the command in Ajax.

What you want is when you press a button, a file is download, exact ? 

then my code is good for you (test it...) , it work for me.

The only thing you need to be aware is to perhaps encode the path in URL mode (because it is send in GET if your form is in ajax mode).

Also you can change you controller code like this :

public ActionResult GetDocument(String path)
{
    var documentStream = _customContext.GetFile(path);
 
    return File(documentStream, System.Net.Mime.MediaTypeNames.Application.Octet, "TestDocument.txt");
}

Regards,
0
Sam
Top achievements
Rank 1
answered on 02 Mar 2012, 05:19 PM
Thanks, this worked.

It still kind of sucks that the entire page has to be submitted, but at least it's working.
Tags
Grid
Asked by
Sam
Top achievements
Rank 1
Answers by
Sam
Top achievements
Rank 1
Dadv
Top achievements
Rank 1
Share this question
or