0

Hi

now, when i download the image, the path will be in this format

C:\Users\itdept\Desktop\webmaster\image\Penguins.jpg

how can i make it save only the filename

Penguins.jpg

?

this is my code

if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
        {
            byte[] imageSize = new byte[FileUpload1.PostedFile.ContentLength];
            HttpPostedFile uploadedImage = FileUpload1.PostedFile;
            uploadedImage.InputStream.Read(imageSize, 0, (int)FileUpload1.PostedFile.ContentLength);
            string ImagePath = FileUpload1.PostedFile.FileName.Trim();

Edited by new SE: n/a

3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by crishjeny
0

Hi..

you can do something like this for save file into sql server database.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Data.SqlClient;

public partial class Add_Books : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (User.Identity.IsAuthenticated == false)
        {
            Server.Transfer("~/Login.aspx");
        }
        if (Roles.IsUserInRole("Administrators") == false)
        {
            Server.Transfer("~/member_Redirect.aspx");
        }
    }
    SqlConnection connection;
    protected void AddBook_Click(object sender, EventArgs e)
    {
        System.Threading.Thread.Sleep(3000);
        if (File1.PostedFile != null) //Checking for valid file
        {

            string StrFileName = File1.PostedFile.FileName.Substring(File1.PostedFile.FileName.LastIndexOf("\\") + 1);
            string StrFileType = File1.PostedFile.ContentType;
            int IntFileSize = File1.PostedFile.ContentLength;

            if (IntFileSize <= 0)
                //Response.Write(" <font color='Red' size='2'>Uploading of file " + StrFileName + " failed </font>");
                Label2.Text = " <font color='Red' size='2'>Uploading of file " + StrFileName + " failed </font>";
            else
            {
                File1.PostedFile.SaveAs(Server.MapPath("~/Books/" + StrFileName));
                //Response.Write("<font color='green' size='2'>Your file " + StrFileName + " of type " + StrFileType + " and size " + IntFileSize.ToString() + " bytes was uploaded successfully</font>");
                Label1.Text = (("~/Books/" + StrFileName));
            }
        }

        if (File2.PostedFile != null) //Checking for valid file
        {

            string StrFileName = File2.PostedFile.FileName.Substring(File2.PostedFile.FileName.LastIndexOf("\\") + 1);
            string StrFileType = File2.PostedFile.ContentType;
            int IntFileSize = File2.PostedFile.ContentLength;

            if (IntFileSize <= 0)
                //Response.Write(" <font color='Red' size='2'>Uploading of file " + StrFileName + " failed </font>");
                ErrorLabel.Text = " <font color='Red' size='2'>Uploading of file " + StrFileName + " failed </font>";
            else
            {
                File2.PostedFile.SaveAs(Server.MapPath("~/Books/" + StrFileName));
                //Response.Write("<font color='green' size='2'>Your file " + StrFileName + " of type " + StrFileType + " and size " + IntFileSize.ToString() + " bytes was uploaded successfully</font>");
                Label2.Text = (("~/Books/" + StrFileName));
            }
        }
        try
        {
            string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            connection = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand("insert into Books (BookName, BookInfo, BookUrl, BookImage, PostedDate, PostedIp, PostedUser) values (@BookName, @BookInfo, @BookUrl, @BookImage, @PostedDate, @PostedIp, @PostedUser)", connection);
            cmd.Parameters.Add("@BookName", SqlDbType.NVarChar).Value = BookName.Text;
            cmd.Parameters.Add("@BookInfo", SqlDbType.NVarChar).Value = BookInfo.Text;
            cmd.Parameters.Add("@BookUrl", SqlDbType.NVarChar).Value = Label1.Text;
            cmd.Parameters.Add("@BookImage", SqlDbType.NVarChar).Value = Label2.Text;
            cmd.Parameters.Add("@PostedDate", SqlDbType.DateTime).Value = DateTime.Now.ToString();
            cmd.Parameters.Add("@PostedIp", SqlDbType.NVarChar).Value = Request.UserHostAddress.ToString();
            cmd.Parameters.Add("@PostedUser", SqlDbType.VarChar).Value = User.Identity.Name.ToString();
            connection.Open();
            int rowsAffected = cmd.ExecuteNonQuery();
            if (rowsAffected != 0)
            {
                AddBookLabel.Text = "Book Added successful";
            }
            else
            {
                AddBookLabel.Text = "An error was occurred";
            }


        }
        catch (SqlException ex)
        {

            string dbErr = "";
            for (int i = 0; i < ex.Errors.Count; i++)
            {
                dbErr += ex.Errors[i].ToString();
            }
            Response.Write(dbErr);
        }
        finally
        {
            string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            connection.Close();
        }

    }
}

downloading you can use handler files like this

<%@ WebHandler Language="C#" Class="VideoHandler" %>

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

public class VideoHandler : IHttpHandler
{
   
    public void ProcessRequest (HttpContext context)
    {
        string connectionString = ConfigurationManager.ConnectionStrings["uploadConnectionString"].ConnectionString;

        SqlConnection connection = new SqlConnection(connectionString);
        SqlCommand cmd = new SqlCommand("SELECT Video, Video_Name FROM Videos WHERE ID = @id", connection);
        cmd.Parameters.Add("@id", SqlDbType.Int).Value = context.Request.QueryString["id"];
        try
        {
            connection.Open();
            SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.Default);
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    context.Response.ContentType = reader["Video_Name"].ToString();
                    context.Response.BinaryWrite((byte[])reader["Video"]);
                }
            }
        }
        finally
        {
            connection.Close();
        }
    }
 
    public bool IsReusable
    {
        get {
            return false;
        }
    }

}

hope this helps

and for

Edited by __avd: Added [code] tags.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.