Home  »  Forum  »  ASP.Net  »

How to empty file save in file upload control?

 Posted By : chandru MPosted Date : 18/07/2011 07:14:13 PM Category : ASP.Net Points : 1

Dear all,

am using file upload control saving in database using store procedure
if (fileVideofile.HasFile)
{
//Save File to the proper directory
string filename = Path.GetFileName(fileVideofile.FileName);
fileVideofile.SaveAs(Server.MapPath("UploadFiles/") + filename);
Videofile = "UploadFiles/" + fileVideofile.FileName;

}
else
{
Videofile = "UploadFiles/";
}
cmd.Parameters.AddWithValue("@Uploadimages",Videofile);

how to save empty file in fileupload control

Advance Thanks to all

chandru

Responses
 Author : Manoranjan Sahoo Member Level : BronzeDate : 18/07/2011 07:24:59 PM Points : 2

Hello,

Do you want to save the empty file or noimage file in a folder or in database?

please clear your query.

 



 Author : chandru M Member Level : BronzeDate : 18/07/2011 07:26:33 PM Points : 2

in database or folder



 Author : Manoranjan Sahoo Member Level : BronzeDate : 18/07/2011 07:27:26 PM Points : 2

no need to save the empty file in database. You can check wheather the file exist or not when fetching the image from database and if not exist then you can show the noimage file.

if you have any problem then let me know.

 



 Author : Manoranjan Sahoo Member Level : BronzeDate : 19/07/2011 07:48:08 AM Points : 2

Hello Chandru,

please check the below code for handling noimage at the time of fetching the image from database :

ShowImageFile.ashx

<%@ WebHandler Language="C#" class="ShowImageFile" %>

using System;
using System.Web;
using System.IO;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Drawing;

public class ShowImageFile : IHttpHandler
{
    public void ProcessRequest (HttpContext context)
    {
        try
        {

            #region commented of DB fetching code
            SqlConnection myConnection = new SqlConnection(ConfigurationManager.AppSettings["sqlconnstr"].ToString());
            if (myConnection.State == ConnectionState.Closed)
            {
                myConnection.Open();
            }
            string sql = "Select imagefile from Table1 where id='" + context.Request.QueryString["id"] + "' and imagefile is not null";
            SqlCommand cmd = new SqlCommand(sql, myConnection);
            cmd.Prepare();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                if (dr["imagefile"] != null && dr["imagefile"].ToString() != "" && (byte[])(dr["imagefile"]) != null)
                {
                    context.Response.BinaryWrite((byte[])dr["imagefile"]);
                }
               
            }
            if (!dr.HasRows)
            {
                context.Response.ContentType = "image/jpg";
                context.Response.WriteFile("~/img/noimage.jpg");
            }
           
            dr.Close();
            if (myConnection.State == ConnectionState.Open)
            {
                myConnection.Close();
            }
            #endregion
                      
        }
        catch
        {
            context.Response.ContentType = "image/jpg";
            context.Response.WriteFile("~/img/noimage.jpg");
        }
}
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}



Post Reply
Login to post Response.