HTML
progetto webform asp.net sial il file html che ashx sono nella stessa root
<!DOCTYPE html>
<html>
<head>
<title>Upload Page</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function () {
$("#Button1").click(function (evt) {
var formData = new window.FormData();
var file = $('#file')[0].files[0];
if (file == null) {
alert("Please select a file to upload...");
return;
}
formData.append("file", file);
formData.append("param01", 10);
formData.append("param02", 20);
formData.append("param03", 30);
$.ajax(
{
url: "fhandler.ashx", // url: "http://w8-paolofu/fUpload/fhandler.ashx",
type: 'POST',
complete: function () {
alert("Upload complete");
},
progress: function (evt) {
//progress event
},
beforeSend: function (e) {
},
success: function (e) {
alert("Upload done");
},
error: function (e) {
alert("Upload Error");
},
///Form data
data: formData,
cache: false,
contentType: false,
processData: false
});
});
});
</script>
</head>
<body>
<form id="form1" runat="server" method="post" enctype="multipart/form-data">
<input name="file" id="file" type="file" />
<input id="Button1" name="Button1" value="Submit" type="button" />
</form>
</body>
</html>
-------------------------------------
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
namespace NsHandler
{
public class FHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string param01 = context.Request.Form["param01"];
string param02 = context.Request.Form["param02"];
string param03 = context.Request.Form["param03"];
if (context.Request.Files.Count > 0)
{
HttpFileCollection files = context.Request.Files;
for (int i = 0; i < files.Count; i++)
{
HttpPostedFile file = files[i];
string fileName = Path.GetFileName(file.FileName);
string fname = context.Server.MapPath("~/tempuploads/" + fileName);
file.SaveAs(fname);
}
}
context.Response.ContentType = "text/plain";
context.Response.Write("File(s) Uploaded Successfully!");
}
public bool IsReusable
{
get
{
return false;
}
}
}
}