Home
About
Contact
Categories
Classic ASP (28 - Sub-Categories)
CSS (1 - Sub-Category)
JavaScript (4 - Sub-Categories)
Databases (22 - Sub-Categories)
ASP.NET (23 - Sub-Categories)
Delphi (5 - Sub-Categories)
Windows Server Core (3 - Sub-Categories)
VMWare (1 - Sub-Category)
Code Editing Tools (2 - Sub-Categories)
Linux (2 - Sub-Categories)
Dell Servers (15 - Sub-Categories)
Bug Reports
(Bugs Fixed
New CFFCS Coding Source is still in Beta
Please report any errors to the [Contact] page. Thank you.
Classic ASP (28)
CSS (1)
JavaScript (4)
Databases (22)
ASP.NET (23)
Delphi (5)
Windows Server Core (3)
VMWare (1)
Code Editing Tools (2)
Linux (2)
Dell Servers (15)
Resources
[View The Source Code For This Project]
Format SQL Script
ASP.NET
C#
C# - Upload Multiple Files with Thumbnail, Folders, and SQL Server Database
HTML
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Load.aspx.vb" Inherits="Load" %>
CSS
* { box-sizing: border-box; } /* Container for flexboxes */ .row { display: flex; flex-wrap: wrap; padding-bottom:10px; } /* Create four equal columns */ .column { padding: 5px; border:1px double #000; } div.column:nth-of-type(1){ flex: 0.5; } div.column:nth-of-type(2){ flex: 1; } div.column:nth-of-type(3){ flex: 2; } /*Add in additional columns, uncomment the below, and add more. Numbering accordingly.*/ /* div.column:nth-of-type(4){ flex: 3; }*/ /* On screens that are 992px wide or less, go from four columns to two columns */ @media screen and (max-width: 992px) { .column { flex: 50%; } } /* On screens that are 600px wide or less, make the columns stack on top of each other instead of next to each other */ @media screen and (max-width: 600px) { .row { flex-direction: column; } }
JavaScript
Upload.js
$(function () { $("[id*=fuUpload]").change(function () { if (typeof (FileReader) != "undefined") { var dvPreview = $("#dvPreview"); dvPreview.html(""); var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.jpg|.jpeg|.gif|.png|.bmp)$/; $($(this)[0].files).each(function () { var file = $(this); // Here is where we check for the file size. This demo is set to 1mb. // Change it to whatever size you need for your project. // Change 1000 / 1000 -- to 5000 / 5000 = 5 in maxSize const size = (file[0].size / 1000 / 1000).toFixed(2); const theSize = size + (size > 1 ? "mb" : "kb"); var maxSize = 1 // Change this to the max allowed to upload. 1 = 1mb / 5 = 5mb / 10 = 10mb / 20 = 20mb if (regex.test(file[0].name.toLowerCase())) { var reader = new FileReader(); reader.onload = function (e) { var strImage = e.target.result if (size > maxSize) { dvPreview.append("File size is larger than the allowed size. (Fize Size =" + theSize + ")
Please upload less than " + maxSize + "mb per image.
Upload button has been diabled.
Please change file size to meet the requirements,
Refresh and Re-Upload files.") $("#btnUpload").prop('disabled', true); } else { dvPreview.append('
' + '
' + theSize + '
' + '
' + '
Title:
' + '
'); } } reader.readAsDataURL(file[0]); } else { dvPreview.html(file[0].name + " is not a valid image file."); return false; } }); } else { alert("This browser does not support HTML5 FileReader."); } }); });
SQL
ImageServer
-- Taken from https://www.cffcs.com/Code/1013 -- Create a new database called [Virtual-Class-01] in SQL Server. -- Right-click and choose [New Query] -- Copy and paste the code below and hit [Execute] USE [Virtual-Class-01] GO /****** Object: Table [dbo].[ImageUpload] Script Date: 6/12/2022 2:55:37 AM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[ImageUpload]( [PicsID] [int] IDENTITY(1,1) NOT NULL, [PicsFolder] [nvarchar](50) NULL, [Userid] [int] NULL, [DefaultFolder] [int] NULL, [PicsPath] [varchar](255) NULL, [PicsName] [nvarchar](50) NULL, [PicsSize] [int] NULL, [PicWH] [varchar](50) NULL, [PicDate] [datetime] NULL, [DateUpdated] [datetime] NULL, CONSTRAINT [PK_ImageUpload] PRIMARY KEY CLUSTERED ( [PicsID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY] GO ALTER TABLE [dbo].[ImageUpload] ADD CONSTRAINT [DF_ImageUpload_PicsFolder] DEFAULT ('Main') FOR [PicsFolder] GO ALTER TABLE [dbo].[ImageUpload] ADD CONSTRAINT [DF_ImageUpload_DefaultFolder_1] DEFAULT ((1)) FOR [DefaultFolder] GO ALTER TABLE [dbo].[ImageUpload] ADD CONSTRAINT [DF_ImageUpload_PicsPath] DEFAULT ('default.jpg') FOR [PicsPath] GO ALTER TABLE [dbo].[ImageUpload] ADD CONSTRAINT [DF_ImageUpload_PicsName] DEFAULT ('Enter a name for this image') FOR [PicsName] GO ALTER TABLE [dbo].[ImageUpload] ADD CONSTRAINT [DF_ImageUpload_PicsSize] DEFAULT ((0)) FOR [PicsSize] GO ALTER TABLE [dbo].[ImageUpload] ADD CONSTRAINT [DF_ImageUpload_PicWH] DEFAULT ('85x84') FOR [PicWH] GO ALTER TABLE [dbo].[ImageUpload] ADD CONSTRAINT [DF_ImageUpload_PicDate] DEFAULT (getdate()) FOR [PicDate] GO ALTER TABLE [dbo].[ImageUpload] ADD CONSTRAINT [DF_ImageUpload_DateUpdated] DEFAULT (((1)/(1))/(2017)) FOR [DateUpdated] GO
ASP.NET
Load.aspx.cs
web.config
database.config
using System; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.IO; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; using System.Security; using System.Text; using System.Threading.Tasks; using Microsoft.VisualBasic; using System.Web; using System.Drawing.Imaging; using System.Drawing; using System.Drawing.Drawing2D; using System.Data; using System.Data.OleDb; using System.Data.SqlClient; using System.Configuration; public partial class Load : System.Web.UI.Page { public class GlobalVariables { public static string MovePath; } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string strid = Request.QueryString["ID"]; if (strid != null && strid != string.Empty) { IDLabel.Text = "Page set to Upload!"; fuUpload.Enabled = true; btnUpload.Enabled = true; } else { IDLabel.Text = "
Load ID into Query
to enable upload."; fuUpload.Enabled = false; btnUpload.Enabled = false; } } } protected void Upload(object sender, EventArgs e) { { var cn = new SqlConnection(); cn = new SqlConnection(ConfigurationManager.ConnectionStrings["Virtual-Learning"].ConnectionString); cn.Open(); GlobalVariables.MovePath = Context.Server.MapPath("ImageServer/"); string strid = Request.QueryString["ID"]; string getun = "cffcoding"; // this will come from a cookie try { for (int i = 0, loopTo = Request.Files.Count - 1; i <= loopTo; i++) { HttpPostedFile postedFile = Request.Files[i]; if (postedFile.ContentLength > 0) { // =-=-=-=-=-=-=-=-Code starts here-=-=-=-=-=-=-=-= var bytes = new byte[postedFile.InputStream.Length]; string GetNames = Request.Form["ImageTitle"]; string[] GetName = GetNames.Split(','); string ext = Path.GetExtension(postedFile.FileName); string Gallery = GlobalVariables.MovePath + getun + "/"; string thPath = ""; string thImageFolder = ""; string theImageFolder = ""; string savethPath = ""; string tempPath = ""; string savepath = ""; var now = DateTime.Now; string getYear = now.ToString("hh") + "" + now.ToString("mm") + "" + now.ToString("ss") + "" + now.ToString("mm") + "" + now.ToString("dd") + "" + now.ToString("yyyy") + now.ToString("fff") + ""; tempPath = Gallery + @"\" + ConfigurationManager.AppSettings["BigFolderPath"]; // Big Folder thPath = Gallery + @"\" + ConfigurationManager.AppSettings["thPath"]; // Thumb Folder thImageFolder = Gallery + @"\" + ConfigurationManager.AppSettings["ImageFolderPath"]; string changeSize = GlobalVariables.MovePath + getun + @"\" + ConfigurationManager.AppSettings["thPath"]; theImageFolder = thImageFolder; // ImageServer/ Folder // savethPath = context.Server.MapPath(thPath) ' + getun savethPath = Gallery + @"\" + ConfigurationManager.AppSettings["thPath"]; // "ImageServer\carrzkiss\Thumbs\" savepath = tempPath; string getFile = getYear + ext; // + clear 'And + clear if (!Directory.Exists(theImageFolder)) { Directory.CreateDirectory(theImageFolder); } if (!Directory.Exists(savethPath)) { Directory.CreateDirectory(savethPath); } // Get the Height and Width of the image and send it to the database. Image GetImageWH; GetImageWH = Image.FromStream(postedFile.InputStream); int PicW = GetImageWH.Height; int PicH = GetImageWH.Width; string PicWH = PicH + "x" + PicW; postedFile.SaveAs(savepath + @"\" + getFile); Image ImageToSave = resizeImage(Image.FromStream(postedFile.InputStream), new Size(130, 110)); ImageToSave.Save((Path.Combine(thPath, getFile.Insert(getFile.LastIndexOf('.'), ""))), ImageFormat.Jpeg); Context.Response.StatusCode = 200; SqlCommand objCmd = new SqlCommand(); string strSQL; strSQL = "INSERT INTO ImageServer(PicsPath, PicsSize, PicWH, UserID, PicsName)VALUES(@PicsPath, @PicsSize, @PicWH, @UserID, @PicsName)"; objCmd = new SqlCommand(strSQL, cn); objCmd.Parameters.Add(new SqlParameter("@PicsPath", getFile)); objCmd.Parameters.Add(new SqlParameter("@PicsSize", bytes.Length)); objCmd.Parameters.Add(new SqlParameter("@PicWH", PicWH)); objCmd.Parameters.Add(new SqlParameter("@Userid", strid)); objCmd.Parameters.Add(new SqlParameter("@PicsName", GetName[i])); objCmd.ExecuteNonQuery(); Response.Write(getFile + "(" + GetName[i] + ") Uploaded Successfully!
"); // =-=-=-=-=-=-=-=-Code ends here=--=-=-=-=-=-=-=-= } // postedFile.ContentLength } } catch { Response.Write("Seems there was an issue, please try uploading your file again."); } finally { cn.Close(); } } } private static Image resizeImage(Image imgToResize, Size size) { int sourceWidth = imgToResize.Width; int sourceHeight = imgToResize.Height; float nPercent = 0f; float nPercentW = 0f; float nPercentH = 0f; nPercentW = size.Width / (float)sourceWidth; nPercentH = size.Height / (float)sourceHeight; if (nPercentH < nPercentW) { nPercent = nPercentH; } else { nPercent = nPercentW; } int destWidth = (int)Math.Round(sourceWidth * nPercent); int destHeight = (int)Math.Round(sourceHeight * nPercent); var b = new Bitmap(destWidth, destHeight); Graphics g = Graphics.FromImage((Image)b); g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.DrawImage(imgToResize, 0, 0, destWidth, destHeight); g.Dispose(); return (Image)b; } }
Resources
(Copy JS and CSS code below, and add it to your project. It is best to download and host from within your site. This will reduce bandwidth drain from your hosting server and the servers hosting the actual files.)
https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
Preview
Tags
C# upload script
C# Images are resized
C# Creating multiple folders
C# Upload Multiple Files