Link to home
Start Free TrialLog in
Avatar of tjgilbert
tjgilbert

asked on

ASP.Net Open/Save dialog box not saving PDF documents correctly.

I am using ASP.Net Open/Save dialog box not saving PDF documents correctly.  The file acts like it's being saved but the size is only 1KB and the orignal file size is 10KB.  

This is my code:

 Dim root As String = "C:\"
  Dim filepath As String = Request.Params("file")
  If Not filepath Is Nothing Then
    If File.Exists(filepath)  Then
      Dim filename As String = Path.GetFileName(filepath)
      Dim newFile = root + filename
      Response.Clear()
      Response.ContentType = "application/pdf"
      Response.AddHeader("Content-Disposition", _
        "attachment; filename=""" & filename & """")
     ' Response.WriteFile(newFile)
     ' Response.Flush()
     ' Response.End()  
    End If
  End If


I apprecate any help you can give me.
Avatar of sandip132
sandip132
Flag of Japan image

Try this:

File.Copy(SourcePath,DestinationPath)
Avatar of deanvanrooyen
deanvanrooyen

try this

            string fileName  = Request.Params["file"];
            string ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
            Response.Charset = "UTF-8";
            Response.ContentType = ContentType;

            Response.WriteFile("../uploads/pdf/" + fileName);
            Response.End();
Avatar of tjgilbert

ASKER

Both suggestions yeid this error:
Security Exception
Description: The application attempted to perform an operation not allowed by the security policy.  To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

Exception Details: System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

I am setup as an IIS server.  So, I am not sure how to download to my hard drive.

Any suggestions?  Thanks for the help so far.
I tried it from another computer that is not setup as a server and got the same error message.
hi, this makes sense, if you are trying to access a file outsite the app domain (eg virtual directory) then it will throw an error -  try and right click the folder and give the asp.net user on the pc permissions to the folder...
Hi deanvanrooyen,
Under the Secuity tab of the directory I have added all abilities to all the Users.  Under the web sharing tab I have made the directory sharable.  The general tab still has readonly checked - even though I uncheck it a serveral times and applied.

Currenty the files are being created but the error message is being stored in them NOT the file data.

Could it be a secuirty issue on the ISP part?  I can see the files just fine. I upload them fine and view them with FTP just fine.

I have even tried to change Secuirty Wizard to set FULL access on everything (as dangerous as that was) it did not help.  so, I put all the default settings back.

I am at a loss here but I appreciate your help.
Hi deanvanrooyen,
 This has become an Emergency.  I think I'm going to loose my job if I don't figure this out by tomorrow.  Is there anyone you can consult to help us?  

The file being created and the error message is being stored in the file.

Please help me.

tjgilbert
SOLUTION
Avatar of deanvanrooyen
deanvanrooyen

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The ISP is external to my organization - we are very small.  I have been working on this problem for soo long everyone is getting impatient with me.  :(

I have not been able to get it  working on my local dev box because I am trying to access the file from the ISP.  On the ISP side I  had setup a directory called documents.  I have given everyone all permissions.  I am able to create new directories under the documents directory and I am able to upload files to the newly created directories.  I can verify these files were uploaded correctly by using FTP and viewing them.

I highlighted the directory documents and right click on it.  I select the security tab on the documents directory I have the Network_Service,  IUSR_4444444  I have given all permissions: Write, Modify, Read, Read and Execute.  For Folders, subfolders and files.

My latest code looks like this:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<%@ IMPORT namespace="System.Data" %>
<%@ IMPORT namespace="System.Web.HttpServerUtility" %>
<%@ Import Namespace="System.IO"%>

<head>
<title>File Download</title>

<script language="VB" runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
 
  Dim filepath As String = Request.Params("file")
  If Not filepath Is Nothing Then
    If File.Exists(filepath)  Then
     Dim filename As String = Path.GetFileName(filepath)
     Response.Clear()
     Response.ContentType = "application/octet-stream; name=" + filename      
     Response.AddHeader("Content-Disposition","attachment; filename=" + filename)            
     Response.Flush()
     Response.WriteFile("C:\downloadedDocuments\" + filename)         
     Response.End()
    End If
  End If

End Sub
</script>

I am getting a file created in the c:\downloadedDocuments directory but when I view the document it has the permission error.

I have tried to set the all the users to have full access on this directoy on my local machine.  

Thank you for trying to help me.  If you can give me step by step stuff to check that would be great.
I changed the code to do a File.Copy.      File.Copy("C:\\databaseInfo.txt", "C:\\databaseInfo111.txt", True)
The code calling the File.Copy is located on the ISP server.
I am stilll getting this permisson error:

Server Error in '/' Application.
--------------------------------------------------------------------------------

Security Exception
Description: The application attempted to perform an operation not allowed by the security policy.  To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

Exception Details: System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

Source Error:


Line 16:      Dim filename As String = Path.GetFileName(filepath)
Line 17:     ' File.Copy(filepath,strPath)
Line 18:      File.Copy("C:\\databaseInfo.txt", "C:\\databaseInfo111.txt", True)
Line 19:    '  Response.Clear()
Line 20:    '  Response.ContentType = "application/octet-stream; name=" + filename      
 
Any Ideas?
Okay, I think part of my problem is that I am trying to access directories on client side when the code is located on the server and is not JavaScript code.
File.Copy("C:\\databaseInfo.txt", "C:\\databaseInfo111.txt", True)  would try to access the C: drive on the server - correct?
How would I access  the client's C: directory?

http://localhost/C://databaseInfo.txt??

It's not a good thing to access the clients directory. I doubt there is anything out there apart from ActiveX etc that would let you access the clients file structure from a browser.
You can browse the clients directory using the FileUpload control to choose the file to upload. But if you want to open files from the browser then that's not adviseable vis a browser.
Thank you both for your help.  The simple solution:  FileWrite parameter is the location of the file being written not the location of where I wanted the file written.