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.
tjgilbertAsked:
Who is Participating?
 
apgourlayConnect With a Mentor Commented:
If the ISP is external to your organisation then you will probably need to ask them directly to make a folder read/write. I've had the same problem before. If you have full access try giving the IUSER_[Machine Name] read write along with any ASP users (ASP.NEt, IIS_WPG, Internet Guest, LaunchIIS Process.
0
 
sandip132Commented:
Try this:

File.Copy(SourcePath,DestinationPath)
0
 
deanvanrooyenCommented:
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();
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
tjgilbertAuthor Commented:
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.
0
 
tjgilbertAuthor Commented:
I tried it from another computer that is not setup as a server and got the same error message.
0
 
deanvanrooyenCommented:
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...
0
 
tjgilbertAuthor Commented:
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.
0
 
tjgilbertAuthor Commented:
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
0
 
deanvanrooyenConnect With a Mentor Commented:
sorry If I consult then I will loose my job :(

if there is an error being written to the file then the issue could be with the code that is creating the file as the file is being created then it can be written to the directory. also to bear in mind working with ISP, unless you have the server as a dedicated server eg you can use remte desktop to controll it then you dont have full controll over it...

have you been able to get it ot work on your local dev box?
0
 
tjgilbertAuthor Commented:
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.
0
 
tjgilbertAuthor Commented:
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?
0
 
tjgilbertAuthor Commented:
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??

0
 
apgourlayCommented:
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.
0
 
tjgilbertAuthor Commented:
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.
0
 
tjgilbertAuthor Commented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.