使用javascript从asp页面重定向到csv文件 [英] Redirect to csv file from asp page using javascript

查看:76
本文介绍了使用javascript从asp页面重定向到csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我的网页上有一点问题。在页面上我生成报告。然后,用户可以将报告导出到.csv文件。 csv文件是我遇到的问题是我不能直接到文件。我继续收到javascript错误,说不允许加载本地资源。如何解决这个问题的任何帮助将不胜感激。提前感谢你。在后面的代码我把信息数组放入DataTables然后将信息写入csv文件。然后我尝试使用ajax链接到asp端的文件。这是后面的代码。



Hi all, i have a little problem on my webpage. On page i produce reports. The user can then export the report to a .csv file. The csv file is made the problem i am having is that i can''t direct to the file. I keep on getting a javascript error saying "Not allowed to load local resource". Any help on how to solve this problem would be much appreciated. Thanking you in advance. On the code behind i put arrays of information into DataTables then write the info into a csv file. I then try to link to the file from the asp side using ajax. here is the code behind.

DataTable dt = new DataTable();

            for (int c = 0; c < headers.Length; c++)
            {
                dt.Columns.Add(headers[c], typeof(String));
            }

            for (int r = 0; r < info.Count; r++)
            {

                String[] this_row = info[r];

                dt.Rows.Add(this_row);                           
            
            
            }

            StringBuilder sb = new StringBuilder();

            IEnumerable<string> columnNames = dt.Columns.Cast<DataColumn>().Select(column => column.ColumnName);

            sb.AppendLine(string.Join(",", columnNames));

            foreach (DataRow row in dt.Rows)
            {

                IEnumerable<string> fields = row.ItemArray.Select(field => field.ToString());
                sb.AppendLine(string.Join(",", fields));


            }

            String path = HttpContext.Current.Server.MapPath("~/" + "testcsv" + ".csv");            

            File.WriteAllText(path, sb.ToString());

            Response.Write(path);

            Response.Flush();

            Response.End();







这是函数在asp页面上。






And here is the function on the asp page.

<pre lang="cs">function convert() {

        var xmlhttp;

        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        }
        else {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

                var innerhtml = xmlhttp.responseText;

                window.location = innerhtml;
            }
        }
        xmlhttp.open("GET", "test_csv.aspx?is_ajax=1&function=convert", true);
        xmlhttp.send();
    }

推荐答案

为什么你不强制下载并只是重定向到那个?您只需在页面上有一个链接,它就会显示下载表单,而不是在浏览器中显示。你甚至不需要javascript。



http:// www.haiders.net/post/Force-File-Download-with-ASPNET.aspx [ ^ ]



祝你好运!
Why don''t you force a download and simply redirect to that? You can simply have a link on your page and it will show the download form instead of showing it in the browser. You don''t even need javascript with that.

http://www.haiders.net/post/Force-File-Download-with-ASPNET.aspx[^]

Good luck!


这篇关于使用javascript从asp页面重定向到csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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