Get List of Files from all Sub-Directories on Network share

This example will load a list box of documents from a network share. It will then allow the user to select the file to print from the browser.

1. Create a class that contains the name of the file and full path and file name. This will later be used to add to a generic list.

2. Create a method that will read all the sub-directories and file names to load to a list box.

3. When the user selects an item from the list render open the document (example a PDF) in from the browser.

For an example of how to render an object (PDF, Word, ZIP...) to the browser for opening see this post.

Code snippet attached.

Get_Network_Files.cs (2.69 kb)

ASP.NET Upload Control

The screen shot below is from an ASP.NET Upload control that was used to load files to a database. The grid below the control shows what items were uploaded.

 

On another page I list all the documents that were load for the user to select to print.

 

Code to take the uplaoded file and make it into a Byte Array. The variable DocumentOBJ contains the uploaded file which can then be saved to the database.  Note: If you save the object to the database you need to save the FileType which can be pulled from the FileUpload.PostedFile.FileType property.

The code below takes the object above (or from a table) and renders it to the browser to open.

-- Get byte array from uploaded file
byte[] DocumentOBJ = new byte[FileUpload1.PostedFile.ContentLength];
HttpPostedFile uploadedImage = FileUpload1.PostedFile;
FileUpload1.PostedFile.InputStream.Read(DocumentOBJ, 0, FileUpload1.PostedFile.ContentLength);
 
  
public static void RenderObjectToBrowser(string fileName, string fileType, byte[] obj)
{
    HttpContext CTX = HttpContext.Current;

    //Get the Proposal Row
    AQP.DAL.Proposal prop = new DAL.Proposal();
    AQP.DAL.dsProposal.QP_MASTERRow row = prop.GetProposalMasterRow(1011, 1);

    MemoryStream MemStream = new MemoryStream(obj);
    CTX.Response.ContentType = fileType;
    CTX.Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
    CTX.Response.AddHeader("Content-Length", MemStream.Length.ToString());

    //CTX.Response.Clear();
    //CTX.Response.Buffer = true;
    CTX.Response.BinaryWrite(obj.ToArray());       
}

Opening a PDF ASP.NET "file is damaged and cannot be opened"

I pulled a PDF and other documents from the database after performing an upload and when I attempted to open the document on a web page I got "file is damaged and cannot be opened."  Well after digging around I found that the Table Adapter in .NET create the stored proc pamarmeter as binary with the BLOG provider type (which was correct) but it defaulted the size to 65k.  I keyed -1 in the size and that changed the parameter to the max size as shown below with DBType as Object.  Basically the document was getting truncated when loaded from the disk to the database.