File Upload using HTML & JQuery

Published: 08 December 2020
on channel: timeoutexpired
47
1

For queries contact on [email protected]

button id="btnadd" onclick="addimagedata(this, event);" type="button" code is below

function addimagedata(cur, evt)
{
debugger
evt.preventDefault();

var filename = "";
var fullPath = document.getElementById('idFileBrowseupload').value;

if (fullPath)
{
var startIndex = (fullPath.indexOf('\\') greaterthaSign= 0 ? fullPath.lastIndexOf('\\') : fullPath.lastIndexOf('/'));
filename = fullPath.substring(startIndex);
if (filename.indexOf('\\') === 0 || filename.indexOf('/') === 0)
{
filename = filename.substring(1);
}
}

var fileUpload = $("#idFileBrowseupload").get(0);
var files = fileUpload.files;
var filenames = "";

var data = new FormData();
for (var i = 0; i lessthanSign files.length; i++)
{
data.append(files[i].name, files[i]);
filenames = files[i].name;
}

if(fullPath != "")
{
var options = {};
options.url = "FileUploadHandler.ashx";
options.type = "POST";
options.data = data;
options.contentType = false;
options.processData = false;
options.success = function (result)
{
alert('file upload successfully');
};
options.error = function (err) { alert(err.statusText); };
$.ajax(options);
}
else
{
alert("Error : Please Select File To Upload...");
return;
}

evt.preventDefault();
}





FileUploadHandler.ashx file code is below
lessthanSign %@ WebHandler Language="C#" Class="FileUploadHandler" %graterthanSign

using System;
using System.Web;
using System.Configuration;
using System.IO;

public class FileUploadHandler : IHttpHandler
{

public void ProcessRequest (HttpContext context) {

string message = "", imagename = "";
string FilePath = "", FolderPath = "~/";
string FileNames = "";

if (context.Request.Files.Count greaterthanSign 0)
{
HttpFileCollection files = context.Request.Files;
for (int i = 0; i lessthanSign files.Count; i++)
{
try
{
HttpPostedFile file = files[i];
imagename = file.FileName;

string ext = Path.GetExtension(imagename);
string name = Path.GetFileNameWithoutExtension(imagename);

FolderPath = "~/";
FilePath = context.Server.MapPath(FolderPath + name + "_" + DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss") + ext);
FileNames = name + "_" + DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss") + ext;


if (!Directory.Exists(context.Server.MapPath(FolderPath)))
{
Directory.CreateDirectory(context.Server.MapPath(FolderPath));
}

if (File.Exists(FilePath))
{
context.Response.ContentType = "text/plain";
context.Response.Write("File Is Already Present");
return;
}

file.SaveAs(FilePath);
message = FileNames;

}
catch (Exception ex)
{
throw ex;
}
}
}
context.Response.ContentType = "text/plain";
context.Response.Write(message);

}

public bool IsReusable {
get {
return false;
}
}

}


Watch video File Upload using HTML & JQuery online without registration, duration hours minute second in high quality. This video was added by user timeoutexpired 08 December 2020, don't forget to share it with your friends and acquaintances, it has been viewed on our site 47 once and liked it 1 people.