Solved

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

Posted on 2011-02-11
13
1,524 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
  • 4
  • 3
  • 2
  • +2
13 Comments
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
on which line do you get that error?

report.Export(exportOpts);

???
0
 
LVL 100

Expert Comment

by:mlmcc
Comment Utility
Does the TEST directory exist?

Does the user have WRITE permissions?

mlmcc
0
 
LVL 100

Expert Comment

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

mlmcc
0
 

Author Comment

by:DKFNJ
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 51

Assisted Solution

by:HainKurt
HainKurt earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Restarting auto-close process on DKFNJ's behalf.
 
modus_operandi
EE Admin
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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

762 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

6 Experts available now in Live!

Get 1:1 Help Now