File Upload using HTML & JQuery

Опубликовано: 08 Декабрь 2020
на канале: 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;
}
}

}


Смотрите видео File Upload using HTML & JQuery онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь timeoutexpired 08 Декабрь 2020, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 47 раз и оно понравилось 1 людям.