I'm trying to upload an image without using any of the asp controls. So far I've been trying:
<form id="form1" data-ajax="false" method="post" runat="server" enctype="multipart/form-data">
<input type="file" id="uploadFile" name="uploadFile" runat="server"/>
<input type="submit" class="btn-login" data-corners="true" value="Yükle" onclick="UploadPhoto()" />
</form>
in my .aspx page and in UploadPhoto() method in JQuery:
function UploadPhoto() {
$.ajax({
type: "POST",
url: "IncidentChange.aspx/UploadPhoto",
contentType: "application/json; charset=utf-8",
success: function (msg) {
},
error: function () {
}
});
}
I am posting to C# code behind. However, I could not reach the uploaded item from codebehind.
[WebMethod]
public static string UploadPhoto()
{
try
{
byte[] fileData = null;
using (var binaryReader = new BinaryReader(HttpContext.Current.Request.Files[0].InputStream))
{
fileData = binaryReader.ReadBytes(HttpContext.Current.Request.Files[0].ContentLength);
}
}
catch (Exception ex)
{
}
return null;
}
However Request.Files appears to be a null array. Since the method is static ([WebMethod]) I cannot reach the input control within the method to get the posted file. How can I overcome this issue? Thank you.