Click here to Skip to main content
15,905,420 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnsubmit_Click(object sender, EventArgs e)
    {

        string FilePath = "";
        string[] a = new string[1];
        string FileName = "";

        if (filupload1.FileName.Length > 0)
        {
            FileName = filupload1.FileName;
            FilePath = Server.MapPath(@"~\Files");
            try
            {
                filupload1.SaveAs(FilePath + @"\" + FileName);
            }
            catch { File.Delete(FilePath + @"\" + FileName);
            filupload1.SaveAs(FilePath + @"\" + FileName);
            }

            lbldisplay.Text = "Uploaded successfully";
         }
<pre lang="xml"><%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td><asp:Label id="lblselectfile" Text="Select File" runat="server"></asp:Label></td>
                <td><asp:FileUpload ID="filupload1"  runat="server" /></td>
            </tr>
            <tr>
                <td><asp:Button ID="btnsubmit" Text="Submit" runat="server"
                        onclick="btnsubmit_Click" /></td>
                        <td><asp:Label ID="lbldisplay" runat="server"></asp:Label></td>
            </tr>
            <tr>
                           </tr>
        </table>
    </div>
    </form>
</body>
</html>

Posted
Comments
Sergey Alexandrovich Kryukov 14-Jul-11 2:22am    
Not a question. Why should anyone read your code?
--SA
Mr.Sourav.Maitra 14-Jul-11 6:14am    
Anyone can read your code if you

1. Want to share knowledge/tips&Tricks
2. Resolve Issues
What's your opinion?
Sergey Alexandrovich Kryukov 19-Jul-11 1:14am    
I think there is not interest in it, if OP does not bother to explain the problem and ask any question. "I have to upload..." -- and who should care. So far, you're the only one. Thank you for your enthusiasm.
--SA
lovejeet0707 14-Jul-11 3:06am    
What do you mean by that Sir SAKryukov????
Sergey Alexandrovich Kryukov 19-Jul-11 1:13am    
I mean exactly what I say. OP does not ask any question, express no concerns, just dump out some code. I don't know why reading this code.
--SA

1 solution

Ya what's the problem? My coding style is different. I made a class file for file handling. Normally I instantiate object of that class and call methods. If you need that class, I can put the class's code.
 
Share this answer
 
Comments
SruthiGeejula 14-Jul-11 2:22am    
i will check it.plz post it
Mr.Sourav.Maitra 14-Jul-11 6:11am    
Use the following code in a class then try.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Collections;

///
/// Summary description for fileFolderRelated
///

///
namespace _________________
{
public partial class FileHandler
{

///
/// File can be uploaded to folder through this function
///

/// <param name="BrowerControl">FileInput Type control</param>
/// <param name="SaveLocation">The path of the file to upload to folder</param>
/// <returns>It will return the file name that has already uploaded

public string InsertFileToFolder(System.Web.UI.WebControls.FileUpload BrowserControl,string SaveLocation)
{
string ImageFileName="";
string ImageSaveLocation="";
int status=0;

if (BrowserControl.PostedFile.ContentLength != 0)
{
ImageFileName = System.IO.Path.GetFileName(BrowserControl.PostedFile.FileName.ToString());
ImageSaveLocation=HttpContext.Current.Server.MapPath(SaveLocation) + "\\" + ImageFileName;

if (!System.IO.File.Exists(ImageSaveLocation))
{
BrowserControl.PostedFile.SaveAs(ImageSaveLocation);
}
else
{
System.IO.File.Delete(ImageSaveLocation);
BrowserControl.PostedFile.SaveAs(ImageSaveLocation);
}
}

return ImageFileName;
}

public string InsertFileToFolderWithNewName(System.Web.UI.WebControls.FileUpload BrowserControl, string SaveLocation, string ImageFileName)
{
//string ImageFileName = "";
string ImageSaveLocation = "";
int status = 0;

if (BrowserControl.PostedFile.ContentLength != 0)
{
ImageFileName = ImageFileName + "_" + System.IO.Path.GetFileName(BrowserControl.PostedFile.FileName.ToString());
ImageSaveLocation = HttpContext.Current.Server.MapPath(SaveLocation) + "\\" + ImageFileName;

if (!System.IO.File.Exists(ImageSaveLocation))
{
BrowserControl.PostedFile.SaveAs(ImageSaveLocation);
}
else
{
System.IO.File.Delete(ImageSaveLocation);
BrowserControl.PostedFile.SaveAs(ImageSaveLocation);
}
}

return ImageFileName;
}

///
/// creates a folder in server
///

/// <param name="folderPath">path of the folder where the new folder will be created</param>
/// <param name="folderName">name of the folder that will be created</param>
public void createFolder(string folderPath, string folderName)
{
if(folderPath == "")
{
DirectoryInfo dInfo = new DirectoryInfo(HttpContext.Current.Server.MapPath(folderName));
dInfo.Create();
}
else
{
DirectoryInfo dInfo = new DirectoryInfo(HttpContext.Current.Server.MapPath(folderPath) + "\\" + folderName);
dInfo.Create();
}
}


///
/// get all files in a folder
///

/// <param name="folderPath">full path of a folder</param>
/// <returns>
public Array selectAllFilesFromFolder(string folderPath)
{
string[] allFiles = Directory.GetFiles(folderPath);
return allFiles;
}

//

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900