1

I think what I have may actually be a design issue rather than a technical issue, but either way I've seem to a hit a wall in both my ability and understanding to get past this problem.

I'm creating a reporting web page as part of a standard VB.NET web application. The page is data driven, with jquery making calls to a web service local to the application to get values of elements that are dynamically created in the DOM. Ultimately these dynamic elements and their values are all wrapped up in a JSON object, so that part of the client side is fine.

Right now, I can pass that JSON object to a method in the web service which creates an SQL Data adapter from the values and queries a database. My ultimate goal is to have this client JSON data be used to query the database and construct an excel file to send back to the user, or failing that a CSV file.

It's my understanding (from working with PDF file generation) that this type of function can only be done on the server side of the web application, not even from the web service (i.e. I can't have the web service return an array of bytes and have JavaScript/jQuery present that as a file).

So, how do I go about this? Now I have my JSON object on the client side, where should I be sending it, how, and how do I go about returning my file?

I've read about either storing the JSON in a hidden field and having the server take the elements value and go from there, or using AJAX to send the JSON to the server just before a postback is made to fire off the server-side function to run the query and return the data.

I just need a little guidance, and a little help understanding where to use the hidden field, or what I am actually doing with the pre-postback AJAX call.

Thanks.

Adam H
  • 193
  • 1
  • 4
  • 16

1 Answers1

2

I have done things similar to this in the past using a generic handler(ASHX) page. If you set the content type of the response HttpContext.Response.ContentType to be text/csv and you call

window.location = "handler.ashx";

It will generate the csv file on the server side and will download in the users browser. You could try passing the JSON as a QueryString and parse it in the handler page to do whatever you want on the server side in order to generate the CSV.

Sample code:

Public Sub ProcessRequest(context As HttpContext)
    context.Response.ContentType = "text/csv"
    context.Response.AddHeader("Content-Disposition", "attachment;filename=test.csv")
    Dim json As String = context.Request.QueryString("json")
    Using sw As New StreamWriter(context.Response.OutputStream)
        sw.WriteLine("Test!")
        sw.Flush()
    End Using
End Sub

I'm not sure if setting a hidden field on client side would help as reading it on server-side would probably not reflect the value you set due to the ViewState not recognizing the changes. There may be ways to get around this though.

EDIT: Sorry over read the part that you said the code was VB.Net. Changed the c# code to VB.net

David Esteves
  • 1,614
  • 2
  • 15
  • 22
  • I'll give that a shot, thank you. I think I understand what you're saying I should try. I've never worked with .ashx pages before, but I am sure I can find some tutorial material on line. Do I need to worry about the length of the QueryString when strifying my json object? – Adam H Jan 09 '12 at 19:55
  • The limit will be the same as any limit of a querystring in a URL. I'm not sure if there is one though... Also when reviewing this I realised I missed an important line of code in order for this to work and thats the `context.Response.AddHeader`. I edited my post to include it. – David Esteves Jan 10 '12 at 13:08
  • Thanks for the clarification. I'd added the response.AddHeader line in and when I navigate directly to the .ashx I get a prompt to open/save a .csv file as expected. I've posted another follow-up question [link](http://stackoverflow.com/questions/8806163/getting-csv-from-ashx-after-jquery-post) which you may have an opinion/answer to. I'd appreciate some further input, and thanks for the help thus far. – Adam H Jan 10 '12 at 15:53
  • Follow up question is now [here](http://stackoverflow.com/questions/8822855/getting-file-from-ashx-with-a-jquery-post) – Adam H Jan 11 '12 at 16:28