20 September 2013

Uploading Any File To A Folder Then VIew It Using GridView

Got a simple codes here for those who want to upload files (any files) in a specific folder, then view it in GridView.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FileUploading.aspx.cs" Inherits="FileUploading" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>File Uploading</title>
</head>
<body>
    <form id="form1" runat="server">
    <h1>
        File Uploading....</h1>
    <asp:FileUpload ID="FileUpload1" runat="server" />
    <asp:Button ID="btnUploadFile" runat="server" Text="Upload" OnClick="DataUploading" />
    <hr />
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:BoundField DataField="Text" HeaderText="Files:" />
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:LinkButton ID="btnDownloading" Text="Download" CommandArgument='<%# Eval("Value") %>'
                        runat="server" OnClick="DataDownloading"></asp:LinkButton>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:LinkButton ID="btnDeleting" Text="Delete" CommandArgument='<%# Eval("Value") %>'
                        runat="server" OnClick="DataDeleting" OnClientClick="return confirm ('Are you sure you want to delete this file?')" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    </form>
</body>
</html>


Code Behind

using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.IO;

public partial class FileUploading : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string[] myfilePath = Directory.GetFiles(Server.MapPath("~/UploadedFiles/"));
            List<ListItem> myFiles = new List<ListItem>();
            foreach (string filePath in myfilePath)
            {
                myFiles.Add(new ListItem(Path.GetFileName(filePath), filePath));
            }
            GridView1.DataSource = myFiles;
            GridView1.DataBind();
        }
    }
    protected void DataUploading(object sender, EventArgs e)
    {
        string myFileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
        FileUpload1.PostedFile.SaveAs(Server.MapPath("~/UploadedFiles/") + myFileName);
        Response.Redirect(Request.Url.AbsoluteUri);
    }
    protected void DataDownloading(object sender, EventArgs e)
    {
        string myPath = (sender as LinkButton).CommandArgument;
        Response.ContentType = ContentType;
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(myPath));
        Response.WriteFile(myPath);
        Response.End();
    }
    protected void DataDeleting(object sender, EventArgs e)
    {
        string myOtherPath = (sender as LinkButton).CommandArgument;
        File.Delete(myOtherPath);
        Response.Redirect(Request.Url.AbsoluteUri);
    }
}

No comments:

Post a Comment