将客户端数据传递到服务器以创建 Excel 或 CSV 文件

发布于 2024-12-25 14:14:54 字数 687 浏览 0 评论 0原文

我认为我所遇到的实际上可能是一个设计问题而不是技术问题,但无论哪种方式,我在解决这个问题的能力和理解方面似乎都遇到了障碍。

我正在创建一个报告网页作为标准 VB.NET Web 应用程序的一部分。该页面是数据驱动的,jquery 调用应用程序本地的 Web 服务来获取 DOM 中动态创建的元素的值。最终,这些动态元素及其值都包装在一个 JSON 对象中,因此客户端的这一部分没问题。

现在,我可以将该 JSON 对象传递给 Web 服务中的一个方法,该方法根据值创建 SQL 数据适配器并查询数据库。我的最终目标是让这个客户端 JSON 数据用于查询数据库并构建一个 excel 文件发送回用户,或者失败时生成 CSV 文件。

据我了解(通过使用 PDF 文件生成),这种类型的功能只能在 Web 应用程序的服务器端完成,甚至不能从 Web 服务完成(即我不能让 Web 服务返回字节数组)并让 JavaScript/jQuery 将其呈现为文件)。

那么,我该怎么办呢?现在我的 JSON 对象位于客户端,我应该将其发送到哪里、如何发送以及如何返回我的文件?

我读过有关将 JSON 存储在隐藏字段中并让服务器获取元素值并从那里开始,或者在进行回发以触发服务器端功能之前使用 AJAX 将 JSON 发送到服务器的内容运行查询并返回数据。

我只需要一点指导,并帮助理解在哪里使用隐藏字段,或者我实际上在使用预回发 AJAX 调用做什么。

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

み青杉依旧 2025-01-01 14:14:54

我过去使用通用处理程序(ASHX)页面做过类似的事情。如果您将响应的内容类型 HttpContext.Response.ContentType 设置为 text/csv 并且调用

window.location = "handler.ashx";

它将在服务器端生成 csv 文件并在用户浏览器中下载。您可以尝试将 JSON 作为查询字符串传递,并在处理程序页面中解析它,以便在服务器端执行您想要的任何操作,以生成 CSV。

示例代码:

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

我不确定在客户端设置隐藏字段是否有帮助,因为在服务器端读取它可能不会反映您设置的值,因为 ViewState 无法识别更改。不过,可能有一些方法可以解决这个问题。

编辑:抱歉,阅读了您所说的代码是 VB.Net 的部分。将c#代码更改为VB.net

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文