Finally it worked. Just added a name
property to the <input type="file">
and it worked fine.
jquery or ashx not working
-
01-07-2023 - |
Question
I am using a ajax file upload control.
It is creating this html:
<form action="/pages/handlers/asyncfileuploadhandler.ashx" method="POST" enctype="multipart/form-data" target="ajaxUploader-iframe-1397144545">
<input type="hidden" name="type" value="mindvoice">
<input type="file" id="imageFile" style="display: block; width: 80%; margin-right: auto; margin-left: auto;">
</form>
And the target is:
<iframe width="0" height="0" style="display:none;" name="ajaxUploader-iframe-1397144545" id="ajaxUploader-iframe-1397144545"></iframe>
The form is submitted using .submit()
.
My ashx handler:
<%@ WebHandler Language="C#" Class="AsyncFileUploadHandler" %>
using System;
using System.Web;
using Nettpals.Core;
public class AsyncFileUploadHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
var file = context.Request.Files[0]; // ArgumentOutOfBoundException
}
public bool IsReusable
{
get
{
return false;
}
}
}
I don't know how to handle the file.
I'm using jquery ajaxfileupload
So what should I do to grab the file?
Solution
OTHER TIPS
HTML:
<input type="file" id="uploadFile" name="uploadFile"/>
<input type="button" id="submit" value="Submit" onClick="UploadFile();"/>
JQuery:
<script type="text/javascript">
$(document).ready(function () {
function UploadFile() {
var fileName = $('#uploadFile').val().replace(/.*(\/|\\)/, '');
if (fileName != "") {
$.ajaxFileUpload({ url: 'AjaxFileUploader.ashx',
secureuri: false,
fileElementId: 'uploadFile',
dataType: 'json',
success: function (data, status) {
if (typeof (data.error) != 'undefined') {
if (data.error != '') {
alert(data.error);
} else {
alert('Success');
}
}
},
error: function (data, status, e) {
alert(e);
}
}
)
}
}
});
</script>
For further reference, please see the below link.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow