Solved

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

Posted on 2011-02-11
13
1,539 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
ID: 34875614
on which line do you get that error?

report.Export(exportOpts);

???
0
 
LVL 100

Expert Comment

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

Does the user have WRITE permissions?

mlmcc
0
 
LVL 100

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
 

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 51

Assisted Solution

by:HainKurt
HainKurt 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

863 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

19 Experts available now in Live!

Get 1:1 Help Now