arnabknd4
10/27/2016 - 8:33 AM

File upload using 1) asp.net Webmetohd 2) jquery ajax

File upload using

  1. asp.net Webmetohd
  2. jquery ajax
function fileSaveOne() {
    var file = $(".sectionOneFile").get(0).files[0];
    var r = new FileReader();
    r.onload = function () {
        var binimage = r.result;
        var description = $(".descriptionOne").val();
        var auctionType = $(".auctionSelector1").val();
        var auctionId = $(".auctionName1").val();
        //var Auction
        data = {
            'AuctionCategory': auctionType,
            'Description': description,
            'AuctionId': auctionId,
            'AuctionPositionId':1,
            'Based64BinaryString': binimage
        }
        console.log(data);
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "feature-auction.aspx//SaveFeatureAuction",
            data: JSON.stringify(data),
            beforeSend: function () { $(".section1img").show(); },
            complete: function () {$(".section1img").hide(); },
            dataType: "json",
            success: function (data) {
                $(".section1img").hide();
                $(".SuccessMsgOne").fadeIn(500).show().fadeOut(5000);
                $(".descriptionOne").html() = "";
                $(".auctionSelector1").val() = -1;
                $(".auctionName1").val() = -1;
            },
            error: function (result) {}
        });
    };
    r.readAsDataURL(file);
}
#region FileSave

        [System.Web.Services.WebMethod(EnableSession = true)]
        [System.Web.Script.Services.ScriptMethod()]
        public static string SaveFeatureAuction(string AuctionCategory, string Description, string AuctionId,string AuctionPositionId, string Based64BinaryString)
        {
            string fileName = "";
            string result = "";
            try
            {
                string format = "";
                string path = HttpContext.Current.Server.MapPath("FileUpload/FeatureAuction/");
                string name = DateTime.Now.ToString("hhmmss");

                if (Based64BinaryString.Contains("data:application/zip;base64,"))
                {
                    format = "zip";
                }
                if (Based64BinaryString.Contains("data:;base64,"))
                {
                    format = "zip";
                }
                if (Based64BinaryString.Contains("data:image/jpeg;base64,"))
                {
                    format = "jpg";
                }
                if (Based64BinaryString.Contains("data:image/png;base64,"))
                {
                    format = "png";
                }
                if (Based64BinaryString.Contains("data:text/plain;base64,"))
                {
                    format = "txt";
                }

                string str = Based64BinaryString.Replace("data:image/jpeg;base64,", " ");//jpg check
                str = str.Replace("data:image/png;base64,", " ");//png check
                str = str.Replace("data:text/plain;base64,", " ");//text file check
                str = str.Replace("data:;base64,", " ");//zip file check
                str = str.Replace("data:application/zip;base64,", " ");//zip file check

                byte[] data = Convert.FromBase64String(str);

                //if (format == "zip")
                //{
                    //using (MemoryStream stream = new MemoryStream(data))
                    //{
                    //    using (ZipFile zip = new ZipFile())
                    //    {
                    //        zip.AddEntry("mainContent.zip", stream);
                    //        zip.Save(path + "/file" + name + ".zip");
                    //        result = "file uploaded succesfully";
                    //    }
                    //}
                //}
                if(format == "jpg")
                {
                    MemoryStream ms = new MemoryStream(data, 0, data.Length);
                    ms.Write(data, 0, data.Length);
                    System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
                    image.Save(path + "/Image" + name + ".jpg");
                    fileName = name + ".jpg";
                    result = "image uploaded successfully";
                }
                if (format == "png")
                {
                    MemoryStream ms = new MemoryStream(data, 0, data.Length);
                    ms.Write(data, 0, data.Length);
                    System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
                    image.Save(path + "/Image" + name + ".png");
                    fileName = name + ".png";
                    result = "image uploaded successfully";
                }
                if (format == "jpeg")
                {
                    MemoryStream ms = new MemoryStream(data, 0, data.Length);
                    ms.Write(data, 0, data.Length);
                    System.Drawing.Image image = System.Drawing.Image.FromStream(ms, true);
                    image.Save(path + "/Image" + name + ".jpeg");
                    fileName = name + ".jpeg";
                    result = "image uploaded successfully";
                }
                string connectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnStr"];
                SqlConnection con = new SqlConnection(connectionString);
                string query = "INSERT INTO [AuctionBidPlatform].[dbo].[FeatureAuctions] ([AuctionCategory],[Description],[AuctionId],[ImageName],[AuctionPositionId])VALUES('" + AuctionCategory + "','" + Description + "','" + AuctionId + "','"+ fileName + "','"+ AuctionPositionId+"')";
                con.Open();
                SqlDataAdapter adapter = new SqlDataAdapter(query, con);
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                con.Close();
            }
            catch (Exception ex)
            {
                result = "Error : " + ex;
            }
            return result;
        }
        #endregion