[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1688
  • Last Modified:

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

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
DKFNJ
Asked:
DKFNJ
  • 4
  • 3
  • 2
  • +2
2 Solutions
 
HainKurtSr. System AnalystCommented:
on which line do you get that error?

report.Export(exportOpts);

???
0
 
mlmccCommented:
Does the TEST directory exist?

Does the user have WRITE permissions?

mlmcc
0
 
mlmccCommented:
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.

 
DKFNJAuthor Commented:
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
 
Todd GerbertIT ConsultantCommented:
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
 
Todd GerbertIT ConsultantCommented:
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
 
HainKurtSr. System AnalystCommented:
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
 
DKFNJAuthor Commented:
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
 
Todd GerbertIT ConsultantCommented:
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
 
Todd GerbertIT ConsultantCommented:
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
 
DKFNJAuthor Commented:
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
 
modus_operandiCommented:
Restarting auto-close process on DKFNJ's behalf.
 
modus_operandi
EE Admin
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

  • 4
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now