Viewing uploaded file!!!

Here we go small ASP.NET/C#.NET snippet to view the uploaded file!

Demo.aspx

<form id="form1" runat="server" enctype="multipart/form-data">
    <div>
        <asp:FileUpload runat="server" ID="fileUpload" />
        <asp:Button runat="server" Text="Upload" OnClick="FileUpload_Click" />
        <asp:HyperLink  runat="server" ID="lnkButton" Text="View File" />
    </div>
    </form>

Demo.aspx.cs

protected void FileUpload_Click(object sender, EventArgs e)
       {
           if (fileUpload.HasFile)
           {
               string strPath = Request.PhysicalApplicationPath + "Upload\\" + fileUpload.FileName;
               fileUpload.SaveAs(strPath);
               lnkButton.NavigateUrl = "~/Upload/" + fileUpload.FileName;
           }
       }
Happy Coding!!!

7 thoughts on “Viewing uploaded file!!!

  1. I am have created a webapplication that is run on android mobile. Problem is that when i download a file (.xls or .zip or .ppt) then file is not downloaded only the aspx page as a file is downloaded. plz provide me some help on this asap.

  2. Thanks for prompt response.
    My code:
    GenFunctions objGenFunc = new GenFunctions();
    DataSet ds = new DataSet();
    byte[] bytFile = null;
    string strFileExt = DownloadType;
    string strFileName = “”;
    ds = objGenFunc.GetTSSReportFileData(ReportReqId);

    if (ds.Tables[ds.Tables.Count – 1].Rows[0][“ErrorStatus”].ToString() == “SUCCESS”)
    {
    if (DownloadType == “.PPT”)
    {
    bytFile = (byte[])ds.Tables[0].Rows[1][“FileData”];
    strFileName = ds.Tables[0].Rows[1][“FileName”].ToString();
    strFileName=strFileName.Replace(“.ppt”, “.PPT”);
    }
    else
    {
    if (ds.Tables[0].Rows[0][“FileExt”] == System.DBNull.Value)
    {
    bytFile = (byte[])ds.Tables[0].Rows[0][“FileData”];
    strFileName = ds.Tables[0].Rows[0][“FileName”].ToString() + “.XLS”;
    }
    else if (ds.Tables[0].Rows[0][“FileExt”].ToString().ToUpper() == “ZIP”)
    {
    bytFile = (byte[])ds.Tables[0].Rows[0][“FileData”];
    strFileName = ds.Tables[0].Rows[0][“FileName”].ToString() + “.ZIP”;
    strFileExt = “.zip”;
    }
    else
    {
    bytFile = (byte[])ds.Tables[0].Rows[0][“FileData”];
    strFileName = ds.Tables[0].Rows[0][“FileName”].ToString() + “.XLS”;
    }
    }
    }

    Response.Clear();

    switch (Microsoft.VisualBasic.Strings.LCase(strFileExt))
    {
    case “.jpg”:
    Response.ContentType = “image/jpg”;
    break;
    case “.jpeg”:
    Response.ContentType = “image/jpeg”;
    break;
    case “.ico”:
    Response.ContentType = “image/ico”;
    break;
    case “.jpe”:
    Response.ContentType = “image/jpe”;
    break;
    case “.png”:
    Response.ContentType = “image/png”;
    break;
    case “.gif”:
    Response.ContentType = “image/gif”;
    break;
    case “.psd”:
    Response.ContentType = “image/psd”;
    break;
    case “.bmp”:
    Response.ContentType = “image/bmp”;
    break;
    case “.tif”:
    Response.ContentType = “image/tif”;
    break;
    case “.tiff”:
    Response.ContentType = “image/tiff”;
    break;
    case “.pdf”:
    Response.ContentType = “application/pdf”;
    break;
    case “.doc”:
    Response.ContentType = “application/msword”;
    break;
    case “.vcs”:
    Response.ContentType = “text/x-vCalendar”;
    break;
    case “.xls”:
    //Response.ContentType = “application/x-msexcel”;
    Response.ContentType = “application/octet-stream”;
    break;
    case “.ppt”:
    //Response.ContentType = “application/ms-powerpoint”;
    Response.ContentType = “application/octet-stream”;
    break;
    case “.zip”:
    //Response.ContentType = “application/x-zip-compressed”; //application/octet-stream
    Response.ContentType = “application/octet-stream”;
    break;
    default:
    Response.AddHeader(“Content-Type”, “binary/octet-stream”);
    break;
    }

    if ((bytFile != null))
    {
    Response.AddHeader(“Content-disposition”, “attachment;filename=\”” + strFileName.ToUpper() +”\””);
    Response.BinaryWrite(bytFile);
    Response.End();
    }

Leave a reply to Kishore Kumar Agarwal Cancel reply