?
Solved

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

Posted on 2011-02-11
13
Medium Priority
?
1,617 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 58

Expert Comment

by:HainKurt
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 58

Assisted Solution

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

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!

Question has a verified solution.

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

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

765 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