Solved

Save csv File to Desktop

Posted on 2014-01-14
7
548 Views
Last Modified: 2014-06-30
I have a .NET web app that queries a mySQL DB. I want to send the results to an Excel file/datatable on the users desktop.

Everything seems to work fine, but the file isn't there. But when I run the app a 2nd time, I get an error that the file exists, but there is no file.

Here's my file path & SQL query.

string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),
                                     "dataTable.csv"); 
                                     
string sql3 = "SELECT * FROM table_173 " +     
              " INTO OUTFILE '" + fileName + "' " +
              " FIELDS TERMINATED BY ',' " +
              " ENCLOSED BY '\"' " +
              " LINES TERMINATED BY '\n'; ";

Open in new window

             
             
The following SQL query works fine & the file is there.
             
string sql2 = "SELECT * FROM table_173 " + 
              " INTO OUTFILE 'C:/LH/dataTable.csv' " +
              " FIELDS TERMINATED BY ',' " +
              " ENCLOSED BY '\"' "  + 
              " LINES TERMINATED BY '\n'; ";

Open in new window

0
Comment
Question by:LaserdogLezly
  • 3
  • 3
7 Comments
 
LVL 32

Assisted Solution

by:Big Monty
Big Monty earned 175 total points
ID: 39780292
you're saving the file to the servers desktop, not the users desktop.

you can't save a file automatically to a users desktop, that's a huge security issue.
0
 

Author Comment

by:LaserdogLezly
ID: 39780543
I saved an HTML file to the desktop successfully. What's the difference?


string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),
                                     "feeschedule.html");

         System.IO.StreamWriter file = new System.IO.StreamWriter(fileName);
         file.WriteLine(myHtmlFile);     
         return myHtmlFile;

Open in new window



on edit: I just checked the remote server & there is no file there.
0
 
LVL 32

Expert Comment

by:Big Monty
ID: 39780558
which desktop?
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:LaserdogLezly
ID: 39780741
I saved an HTML file to my desktop.
0
 
LVL 32

Expert Comment

by:Big Monty
ID: 39780953
is this the same machine that hosts the web site?
0
 
LVL 16

Accepted Solution

by:
Vikram Singh Saini earned 175 total points
ID: 39782900
Hi,

It is worthy to read article at link MySQL Reference -13.2.8.1. SELECT ... INTO Syntax

As per this article,

The SELECT ... INTO OUTFILE 'file_name' form of SELECT writes the selected rows to a file. The file is created on the server host, so you must have the FILE privilege to use this syntax. file_name cannot be an existing file, which among other things prevents files such as /etc/passwd and database tables from being destroyed.

My suggestion as solution would be to grant full permission to website code for saving file at the location you specified in your code -

string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),
                                     "feeschedule.html");

Open in new window

0
 

Author Closing Comment

by:LaserdogLezly
ID: 40168517
Both responses were correct in that I was saving to a remote server with permissions.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Creating and Managing Databases with phpMyAdmin in cPanel.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now