Solved

ERROR - System.UnauthorizedAccessException: Access to the path '...' is denied.

Posted on 2011-02-11
13
1,591 Views
Last Modified: 2012-05-11
This is a Web App in c# ASP.NET.

The user selects some criteria, then displays a Crystal Reports Report on the same page.  I include the ability to export to .txt (tab or comma separated).  But, when I try to save the file, I get the following error:

System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Test\02112011.txt'.

(I'm sure you know but 'C:\Test\02112011.txt' is the file name on the local machine that I'm trying to save it to)

Someone had mentioned sending the file to the browser and making the user select a save location that way.  How do I go about that?  I want the page to still display the selection criteria and the Crystal Report.


Here's the code:

        string ExportFile = "";
        ExportFile = txtPath.Text.ToString();

        float IsTxt = 0;
        float Isfolder = 0;

        IsTxt = ExportFile.IndexOf(".txt");
        Isfolder = ExportFile.IndexOf(":\\");

        if (IsTxt < 0)
        {
            ExportFile = ExportFile + ".txt";
        }

        if (Isfolder >= 0)
        {
            //go through with export            
            BindReport("true");
            DiskFileDestinationOptions diskOpts = ExportOptions.CreateDiskFileDestinationOptions();
            ExportOptions exportOpts = new ExportOptions();

            exportOpts = report.ExportOptions;
            if (rblExportType.SelectedValue.ToString() == "Tab")
            {
                exportOpts.ExportFormatType = ExportFormatType.TabSeperatedText;
            }
            else
            {
                exportOpts.ExportFormatType = ExportFormatType.CharacterSeparatedValues;
            }

            exportOpts.ExportDestinationType = ExportDestinationType.DiskFile;

            diskOpts.DiskFileName = ExportFile;
            exportOpts.ExportDestinationOptions = diskOpts;
            report.Export(exportOpts);

            RegisterStartupScript("startUpScript", "<script language=JavaScript>alert('Export Complete');</script>");
        }
        else
        {
            RegisterStartupScript("startUpScript", "<script language=JavaScript>alert('You Need to Select a .txt file');</script>");



Thank you
0
Comment
Question by:DKFNJ
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
13 Comments
 
LVL 55

Expert Comment

by:Huseyin KAHRAMAN
ID: 34875614
on which line do you get that error?

report.Export(exportOpts);

???
0
 
LVL 101

Expert Comment

by:mlmcc
ID: 34875776
Does the TEST directory exist?

Does the user have WRITE permissions?

mlmcc
0
 
LVL 101

Expert Comment

by:mlmcc
ID: 34875781
Since this is a WEB application the C: may be getting interpreted as the server C: drive.

mlmcc
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:DKFNJ
ID: 34887755
I figured out the error.  The problem is that I need to get the file to the client PC, not the server.  How do I go about doing that?
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 34888096
I have already addressed these issues in your other question, and also included C# examples that send the output of a Crystal Report to the browser as a PDF.  See http:Q_26803432.html#a34834844 and http:Q_26803432.html#a34842836.

I recommend you direct the user's browser to a .ASHX page (an ASHX being a page that just runs code, no HTML, etc), that ASHX page can open the report and send it to the client as an attachment, which should cause the Open/Save/Cancel dialog to open and leave the current page being viewed as it is (or you can always just open the report in a new window).
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 34888134
Here's an example ASHX that checks the query string for the Crystal Report to load, then sends it to the browser as a PDF attachment.

public class DownloadReport : IHttpHandler
{

	public void ProcessRequest(HttpContext context)
	{
		string reportPath = context.Request.QueryString["ReportPath"];

		ReportDocument reportDocument = new ReportDocument();
		reportDocument.Load(reportPath);
		reportDocument.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, true, "YourReport.pdf");
		context.Response.End();
	}

	public bool IsReusable
	{
		get
		{
			return false;
		}
	}

}

Open in new window


You would then direct your user's browser to http://www.yoursite.com/ShowReport.ashx?ReportPath=theReport.rpt, for example.
0
 
LVL 55

Assisted Solution

by:Huseyin KAHRAMAN
Huseyin KAHRAMAN earned 500 total points
ID: 34889746
you cannot write to client machine from server code...
only you can write it to response and client can get the file/download the file...

check this code on how to write a csv file and send to client...

http://wiki.asp.net/page.aspx/401/export-to-csv-file/
0
 

Accepted Solution

by:
DKFNJ earned 0 total points
ID: 34955386
I wound up:
1. creating the txt/csv file on the server, set filePath to its location
2. sending the file to the browser


   File Info targetFile = new FileInfo(filePath);

   Response.AddHeader("Content-Disposition", "attachment; filename=" + targetFile.Name);
   Response.AddHeader("Content-Length", targetFile.Length.ToString());
   Response.ContentType = "application/octet-stream";
   Response.WriteFile(TargetFile.FullName);
   Response.End();


Thanks for all the help, guys.

tgerbert, I apologize for not seeing the comment you had mentioned until after I saw your response here.  Unfortunately, I as unable to use a PDF.  Our documentation specifically stated csv.  Even if it was csv format saved as a PDF, I wouldn't be able to use it.
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 34955438
It doesn't matter - the concept is the same, you need to send bytes to the client's browser, you can't save the content to the server's disk - whether it's a PDF, an Excel workbook, Word document, or CSV file.

If you're wanting to export the report as a CSV, then all you might need to do is change (in the snippet in http:#34888134) line 10 reportDocument.ExportToHttpResponse(ExportFormatType.CSV, Response, true, "YourReport.csv");


Besides, I also gave you an example that exports a CSV.
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 34955479
I provided this answer over a week ago.  See my comments in http:#a34888096.

Since this is a duplicate question I recommend delete without refund.

Otherwise, split points between http:#34888096 and http:#34888134
0
 

Author Comment

by:DKFNJ
ID: 34955573
Actually, the answer to the other thread was more a "you cannot do this in web-based applications," which you did answer:

"You can't use those dialogs in a web app, they're form forms apps."

This question was about something different when it was opened.  It was about why I was receiving an error.  I was receiving the error because it was writing to the server, not the client and that folder only existed on the client.

Both threads did lean toward the same issue at the end.

I did give credit for answering the other question because it was the closest on that thread.  But the person who I gave credit to here, answered along the lines of what I needed more.  I apologize for the confusion.

As I had said, I'm new to this and I appreciate your help.
0
 
LVL 1

Expert Comment

by:modus_operandi
ID: 34995343
Restarting auto-close process on DKFNJ's behalf.
 
modus_operandi
EE Admin
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

688 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