使用jQuery Post从.ashx获取文件 [英] Getting file from .ashx with a jQuery Post

查看:166
本文介绍了使用jQuery Post从.ashx获取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是我问的另一个问题的后续内容-

This question is a following from another question I asked - Passing client data to server to create Excel or CSV File.

我有一个客户端页面,该页面构建了一个JSON对象以发送到服务器,并且我有服务器代码,可以将该JSON对象解析为SQL命令并最终获得所需数据的数据集.

I have a client page which builds a JSON object to send to the server, and I have server code which can parse that JSON object into an SQL command and end up with a dataset of required data.

我最初是将JSON对象传递给.asmx Web服务,该服务将返回包含我的数据的JSON对象.现在,我想朝另一个方向前进,并将数据作为.csv文件返回.

I had originally been passing the JSON object to an .asmx web service which would return a JSON object containing my data. Now I want to go in a different direction and have the data returned as a .csv file.

我了解我可以尝试将JSON对象放入查询字符串中并调用.ashx页面,但是JSON对象可能变大,因此我尝试使用POST的Request.Form.

I understand I can try to put my JSON object into a query string and call my .ashx page, but the JSON object could get large, so I'm trying to use the Request.Form of a POST.

我的问题并且缺乏理解,是关于如何使用jQuery发布到.ashx页面并使它将.csv文件返回给客户端.如果我直接导​​航到.ashx页面(并修改页面以对传递的数据进行硬编码),那么我会得到.csv文件,这没有问题(即,我得到了打开/保存文件的提示).如果我从jQuery对.ashx文件进行POST并通过其发送JSON对象,则会得到一个包含字符串中数据的响应,而不是.csv.

My question, and lack of understanding, is in how to use jQuery to post to the .ashx page and have it return the .csv file to the client. If I navigate to the .ashx page directly (and modify the page to hard code the passed data), I get the .csv file returned to me no problem (i.e I get the prompt to open/save the file). If I make a POST to the .ashx file from jQuery and send my JSON object through, I get a response which contains the data in a string, rather than getting a .csv.

那么,是我缺少了什么,还是我只是想实现我无法或不应该做的事情?

So, is there something I am missing, or am I just trying to achieve something that I can't or shouldn't be doing?

我曾考虑过将JSON对象传递给.asmx Web服务,该服务会将JSON对象存储到数据库中并返回ID,然后使用window.location浏览到ID为查询的.ashx字符串参数,然后生成.csv文件,但我认为可能有一种避免该中间步骤并通过POST执行的方法.

I'd thought about passing my JSON object to a .asmx web service which would store the JSON object into a database and return an ID, and then use window.location to browse to the .ashx with the ID as a query string parameter to then generate the .csv file, but I thought there might be a way to avoid that middle step and do it with the POST.

抱歉,这有点杂乱无章.我很乐意澄清任何可能对任何人都没有意义的部分.

Sorry if this is a little rambling and disjointed. I'll be happy to clarify on any parts that may not make sense to anyone.

推荐答案

我正在尝试做与我们讲类似的事情.

I am trying to do something similar as we speak.

看看这个问题.可能会对您有帮助.

Take a look at this question. It might help you.

方法:jQuery帖子到ashx文件以强制下载结果?

这篇关于使用jQuery Post从.ashx获取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆