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.  

 

Capture Enter Key on ASP.NET TextBox

Here is a method you can pass a text box and a command button to add the onpress event logic.

namespace SURFTHRU
{
    public static class HTMLControls
    {
        public static void SetEnterButton(TextBox txtBox, Button btn)
        {
            txtBox.Attributes.Add("onkeydown",
                      "if(event.which || event.keyCode) { if ((event.which == 13) || (event.keyCode == 13)) " +
                      "{document.getElementById('" + btn.ClientID + "').click(); return false;}}" +
                      "else {return true}; ");
        }

    }
}