Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

C#
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();



And here is the function on the asp page.

JavaScript
<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();
    }
Posted
Updated 23-Apr-13 19:46pm
v3
Comments
E.F. Nijboer 23-Apr-13 10:21am    
What are you doing? I think some example code would help.
Ruwaldo 24-Apr-13 1:44am    
I have updated my question and added some of my code.
ZurdoDev 23-Apr-13 10:35am    
You'll need to post your relevant code.
Ruwaldo 24-Apr-13 1:44am    
I have updated my question and added some of my code.

1 solution

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!
 
Share this answer
 
Comments
Ruwaldo 24-Apr-13 5:06am    
Thanks for link it worked for me.
E.F. Nijboer 24-Apr-13 6:04am    
Great! :-D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900