Wednesday, May 8, 2013

sample code for file upload in asp.net

Here we describe how to upload file ,image, media in asp.net and show in repeate control :
MediaBrowser.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MediaBrowser.aspx.cs" Inherits="Admin_MediaBrowser" %>
<!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></title>
    <style type="text/css">
        body
        {
            margin: 0;
            padding: 0;
            font-size: 10pt;
            font-family: Arial,Tahoma,Verdana;
        }
        .media-upload
        {
            border-top: 1px solid #e7e7e7;
            padding: 10px;
            display: block;
            overflow: auto;
            height:95px;
        }
        .media-images
        {
            display: block;
            height: 220px;
            overflow: auto;
        }
        .media-image-click
        {
            float: left;
            width: 110px;
            height: 130px;
            margin: 5px;
            cursor: pointer;
            display: block;
            border: 2px solid transparent;
            text-align: center;
        }
        .media-image-click:hover
        {
            border: 2px solid #e7e7e7;
        }
        .media-image-click:hover span
        {
            color: #ff0000;
        }
        .media-image-click span
        {
            display: block;
            text-align: center;
            font-size: 10px;
            color: #666;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div class="media-images">
    <asp:Literal runat="Server" ID="ltNoFile"></asp:Literal>
        <asp:Repeater runat="Server" ID="rpFiles">
            <ItemTemplate>
                <div class="media-image-click" onclick="addMedia('<%#Eval("ImageName") %>','<%#Eval("imageid") %>','<%#Eval("show") %>');">
                    <img style="max-width: 100px; max-height: 100px;" src="~/<%#GetThumbnail((int)Eval("show"),(string)Eval("ImageName"),(string)Eval("Code")) %>" />
                    <span>
                        <%#Eval("ImageName")%></span>
                </div>
            </ItemTemplate>
        </asp:Repeater>
    </div>
    <div class="media-upload">
        Add Media Description<br />
        <br />
        <asp:FileUpload runat="server" ID="fupFile" />
        <asp:Button  ID="btnupload" runat="server" onclick="btnupload_Click" Text="Upload"/>
    </div>
    </form>
</body>
</html>



MediaBrowser.aspx.cs :
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Web.Security;
using System.Collections;
using System.Data.SqlClient;
using System.Configuration;
public partial class Admin_MediaBrowser : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connction"].ToString());
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindImage();
        }
    }
    private void BindImage()
    {
        try
        {
         
            string qry = "SELECT * FROM ImageMaster";
            SqlCommand cmd = new SqlCommand(qry, con);
            SqlDataAdapter ad = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            ad.Fill(ds);
          
            rpFiles.DataSource = ds.Tables[0];
            rpFiles.DataBind();
            if (ds.Tables[0].Rows.Count == 0)
                ltNoFile.Text = "<span style=\"padding:5px;margin:5px;text-align:center;display:block;border:1px solid #e7e7e7;background:#f4f4f4;font-weight:bold\">" + "NoMedia</span>";
        }
        catch (System.Exception ex)
        {
            ltNoFile.Text = ex.Message;
        }
    }
    public string GetThumbnail(int sLibraryType, string strName, string strExtension)
    {
        string strThumb = string.Empty;
        if (sLibraryType == 0)
        {
            if (strExtension.Equals("doc") || strExtension.Equals("docx") || strExtension.Equals("pdf") || strExtension.Equals("xls") ||
            strExtension.Equals("xlsx") || strExtension.Equals("txt"))
                strThumb = "Images/FileTypes/document.png";
            else if (strExtension.Equals("zip") || strExtension.Equals("rar") || strExtension.Equals("tar") || strExtension.Equals("7z"))
                strThumb = "Images/FileTypes/zip.png";
            else
                strThumb = "Images/FileTypes/file.png";
        }
        else if (sLibraryType == 2)
            strThumb = "Images/FileTypes/media.png";
        else if (sLibraryType == 1)
            strThumb = "Images" + strName;
        return strThumb;
    }
    protected void btnupload_Click(object sender, EventArgs e)
    {
        System.Collections.Generic.Dictionary<string, string> dicFileTypeToFolder = new System.Collections.Generic.Dictionary<string, string>();
        dicFileTypeToFolder.Add(".jpg", "Images");
        dicFileTypeToFolder.Add(".jpeg", "Images");
        dicFileTypeToFolder.Add(".tif", "Images");
        dicFileTypeToFolder.Add(".png", "Images");
        dicFileTypeToFolder.Add(".gif", "Images");
        dicFileTypeToFolder.Add(".raw", "Images");
        dicFileTypeToFolder.Add(".bmp", "Images");
        dicFileTypeToFolder.Add(".avi", "Medias");
        dicFileTypeToFolder.Add(".mpg", "Medias");
        dicFileTypeToFolder.Add(".mpeg", "Medias");
        dicFileTypeToFolder.Add(".mp4", "Medias");
        dicFileTypeToFolder.Add(".flv", "Medias");
        dicFileTypeToFolder.Add(".wmv", "Medias");
        dicFileTypeToFolder.Add(".mp3", "Medias");
        dicFileTypeToFolder.Add(".wav", "Medias");
        HttpPostedFile file = (HttpPostedFile)fupFile.PostedFile;
        string strExtension = System.IO.Path.GetExtension(file.FileName).ToLowerInvariant();
        string strFileName = System.IO.Path.GetFileName(file.FileName);
        string strUploadPath = string.Empty;
        if (dicFileTypeToFolder.ContainsKey(strExtension))
            strUploadPath = Server.MapPath("~/" + dicFileTypeToFolder[strExtension] + "/");
        else
            strUploadPath =Server.MapPath("~/Files/");
        if (!System.IO.Directory.Exists(strUploadPath))
        {
            System.IO.Directory.CreateDirectory(strUploadPath);
        }
        file.SaveAs(System.IO.Path.Combine(strUploadPath, strFileName));
        string strError = "";
        int type = (short)(dicFileTypeToFolder.ContainsKey(strExtension) ? (dicFileTypeToFolder[strExtension].Equals("Images") ? 1 : 2) : 0);
        int iReturnValue = AddImage(strFileName, type, strExtension.Replace(".", ""));
        BindImage();
      
    }
    public int AddImage(string title, int type, string extension)
    {
        string qry = "insert into imagemaster(imagename,type,extension) values(" + title + "," + type + "," + extension + ")";
        SqlCommand cmd = new SqlCommand(qry, con);
        cmd.ExecuteNonQuery();
    }
}
Source code : Download Source code

If you are searching life partner. your searching end with kpmarriage.com. now kpmarriage.com offer free matrimonial website which offer free message, free chat, free view contact information. so register here : kpmarriage.com- Free matrimonial website

0 comments:

Post a Comment