Solved

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

Posted on 2006-10-30
15
942 Views
Last Modified: 2012-05-05
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.
0
Comment
Question by:tjgilbert
  • 9
  • 3
  • 2
  • +1
15 Comments
 
LVL 12

Expert Comment

by:sandip132
ID: 17838950
Try this:

File.Copy(SourcePath,DestinationPath)
0
 
LVL 12

Expert Comment

by:deanvanrooyen
ID: 17840153
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
 

Author Comment

by:tjgilbert
ID: 17854067
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
 

Author Comment

by:tjgilbert
ID: 17862890
I tried it from another computer that is not setup as a server and got the same error message.
0
 
LVL 12

Expert Comment

by:deanvanrooyen
ID: 17871978
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
 

Author Comment

by:tjgilbert
ID: 17883015
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
 

Author Comment

by:tjgilbert
ID: 17889874
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 12

Assisted Solution

by:deanvanrooyen
deanvanrooyen earned 250 total points
ID: 17904390
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
 
LVL 2

Accepted Solution

by:
apgourlay earned 250 total points
ID: 17906649
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
 

Author Comment

by:tjgilbert
ID: 17907715
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
 

Author Comment

by:tjgilbert
ID: 17910072
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
 

Author Comment

by:tjgilbert
ID: 17910186
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
 
LVL 2

Expert Comment

by:apgourlay
ID: 17914201
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
 

Author Comment

by:tjgilbert
ID: 17950622
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
 

Author Comment

by:tjgilbert
ID: 17950635
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 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

25 Experts available now in Live!

Get 1:1 Help Now