Monday, July 23, 2018

file save in different name if file is exist in same name by using FileUpload control in asp.net | Sradha Webcreations


file save in different name if file is exist in same name by using FileUpload control in asp.net 

check file in same name before store in ms sql server data base 
 http://sradhawebcreations.com

filesaving.aspx

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

<!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>
    <script type="text/javascript" src="jquery-1.8.2.js"></script>

/*Java script for priview image*/

<script type="text/javascript">
    function showimagepreview(input) {
        if (input.files && input.files[0]) {
            var filerdr = new FileReader();
            filerdr.onload = function (e) {
                $('#imgprvw').attr('src', e.target.result);
            }
            filerdr.readAsDataURL(input.files[0]);
        }
    }
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
     <asp:FileUpload ID="FileUpload1" runat="server" onchange="showimagepreview(this)" />
        <br />
  <asp:Button ID="ButtonSAve" runat="server" onclick="ButtonSAve_Click"  Text="Save" />
<br/>
            <img id="imgprvw" />
<asp:Label ID="UploadStatusLabel" runat="server"Text="Label" Visible="False"></asp:Label>
        <br />
   
    </div>
    </form>
</body>
</html>

 http://sradhawebcreations.com

filesaving.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;


public partial class filesaving : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    SqlConnection con=new SqlConnection (ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString);

    protected void ButtonSAve_Click(object sender, EventArgs e)
    {
        try
        {
            string FileName = SaveFile(FileUpload1.PostedFile);
            con.Open();
            SqlCommand cmd = new SqlCommand("insert into DATAIMAGE (image) values(@image)", con);


            cmd.Parameters.AddWithValue("image", "Tested Files/" + FileName);
            cmd.ExecuteNonQuery();
            con.Close();
        }
        catch(Exception ex)
        {
            Response.Write(ex);
           
        }

    }

         string SaveFile(HttpPostedFile file)
    {
        // Specify the path to save the uploaded file to.
      /* string savePath = "D:\\demoData\\demodataa\\Ademodataaa\\Tested Files\\Tested Files\\";*/
        string savePath = "~\\Tested Files\\";

        // Get the name of the file to upload.
        string fileName = FileUpload1.FileName;    // must be declared in the class above

        // Create the path and file name to check for duplicates.
        string pathToCheck = savePath + fileName;

        // Create a temporary file name to use for checking duplicates.
        string tempfileName = "";

        // Check to see if a file already exists with the
        // same name as the file to upload.
        if (fileName.Length == 0)
        {
            fileName = "file Not Exist";
        }
        else
        {
            if (System.IO.File.Exists(Server.MapPath(pathToCheck)))
            {
                int counter = 2;
                while (System.IO.File.Exists(Server.MapPath(pathToCheck)))
                {
                    // if a file with this name already exists,
                    // prefix the filename with a number.
                    tempfileName = counter.ToString() + fileName;
                    pathToCheck = savePath + tempfileName;
                    counter++;
                }

                fileName = tempfileName;

                // Notify the user that the file name was changed.
                UploadStatusLabel.Visible = true;
                UploadStatusLabel.Text = "A file with the same name already exists." +
              "<br />Your file was saved as " + fileName;
            }
        }


        // Append the name of the file to upload to the path.
        savePath += fileName;

        // Call the SaveAs method to save the uploaded
        // file to the specified directory.
        FileUpload1.SaveAs(Server.MapPath(savePath));
        return fileName;
    }
   
}






No comments:

Post a Comment