0

Me have an projects web use the CodeFile on me Browser photos from local up then save the path file Images on Database the successful. But on me developers Project there in the host you can not to insert the path file Images it into Database of me. Although the project on the local machine works very well my. Can someone help me. Thank you all Here is chunk codes of me:

HTML CODE:

<%@ Page Title="" Language="C#" MasterPageFile="~/ad/Admin.Master" AutoEventWireup="true" CodeFile="EditProduct.aspx.cs" Inherits="EditProduct" %>

<asp:Image ID="Image1" runat="server" Height="91px" Width="96px" />
<asp:FileUpload ID="FileUpload1" runat="server" /></td>

Code C#:

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 BAL;
using DAL; 




protected void Insert()
        {
            Product product = new Product();
            //Gan du lieu vao doi tuong 
            product.Pro_Keyword = txtKeyword.Value + " " + txtPro_Name.Value;
            product.Pro_IsDiscount = chkKhuyenMai.Checked;
            product.Pro_Price = int.Parse(txtPrice.Value);
            product.Pro_Name = txtPro_Name.Value;
            product.Pro_Warranty = txtWarranty.Text;
            product.Pro_Condition = txtCondition.Text;
            product.Title = txtTitle.Text;
            product.Keyword = txtGoogle_Keyword.Text;
            product.Description = txtDescription.Text;

            product.Pro_Position = 0;
            product.Pro_PriceDiscount = double.Parse(txtPriceDiscount.Value);
            product.Pro_IsComment = true;
            product.Pro_Content = HtmlEditor2.Text;
            //product.Pro_Intro = HtmlEditor1.Text;
            product.Pro_Intro = txtCode_Product.Text;
            product.Cate_ID = int.Parse(ddlCategory.SelectedValue);

            string _sFolder = Request.PhysicalApplicationPath.Replace(@"\", "/") + "/Images_Upload/Product/";
            product.Pro_CreateDate = DateTime.Now;

            if (string.IsNullOrEmpty(FileUpload1.FileName) == false)
            {
                product.Pro_Image = ExtendFunctional.UploadPicture(_sFolder, FileUpload1.PostedFile);
                Image1.ImageUrl = "../Images_Upload/Product/" + product.Pro_Image;
            }
            //if (string.IsNullOrEmpty(FileUpload2.FileName) == false)
            //{
            //    product.Pro_BigImage = ExtendFunctional.UploadPicture(_sFolder, FileUpload1.PostedFile);
            //    Image2.ImageUrl = "/Images/Products/" + product.Pro_BigImage;
            //}
            product.Pro_PriceDiscount = 0;
            product.Pro_Status = true;
            //Them moi
            Product_BAL.Insert_Product(product);
            Response.Write("<script>alert('Đã thêm mới thành công!') ; window.location.href='ManageProduct.aspx'</script>"); 
            //MessageBox.Show("Đã thêm mới thành công.");

        }

Class ExtendFunctional:

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;


    public class ExtendFunctional
    {
        public static bool IsNumberic(string str)
        {
            try
            {
                if (string.IsNullOrEmpty(str)) return false;
                int i = Convert.ToInt32(str);
                return true;
            }
            catch { return false; }
        }


        public static void DeleteImageInFolder(string path)
        {
            if (System.IO.File.Exists(path))
            {
                System.IO.File.Delete(path);
            }
        }
        public static bool IsImageFile(string fileName)
        {
            try
            {
                string[] extend = new string[] { ".gif", ".bmp", ".jpg", ".jpeg", ".png" };

                string ex = fileName.Substring(fileName.Length - 4).ToLower();
                foreach (string s in extend)
                    if (s == ex) return true;
                return false;
            }
            catch { return false; }
        }
        public static string UploadPicture(string _sFolder, HttpPostedFile _File)
        {
            string _sResult = "";
            if (!System.IO.Directory.Exists(_sFolder))
                System.IO.Directory.CreateDirectory(_sFolder);

            string _sFileName = System.IO.Path.GetFileName(_File.FileName);
            try
            {
                _File.SaveAs(_sFolder + "/" + _sFileName);
                _sResult = _sFileName;
            }
            catch (Exception)
            {

            }
            return _sResult;
        }
    }
4

0 回答 0