I have a page made in HTML and JavaScript , in this I have a form with several inputs
among them is a input file
, and I send them to the server through Ajax and JS .
On the server side I receive the data from the inputs
, but when I receive the input file
I get an incorrect data type error.
My question is: What type of data do I have to use on the server side ( ASP.net and C# ) in order to receive the file correctly.
Here part of the code I have:
HTML
<form id='frm1'>
<input type='text' id='nombre'>
<input type='file' id='balance'>
</form>
javascript
fuction guardar(){
var nombre = $('#nombre').val();
var files = $("#balance").get(0).files;
PageMethods.Guardar(nombre,files[0],guardar_Callback, Failed_CallBack,context);
}
On the server side I have the following method:
[WebMethod()]
public static int guardar(string nombre, tipoArchivo balance){
//lo que haga con la informacion
}
As I told you, with the exception of the data type to receive the file, everything works fine.
After a lot of research and with the mentioned requirements of Using JavaScrip , PageMethods and WebMethod , I must say that it is not possible to upload a file to the server, due to the limitations of the technologies themselves and their way of working.
In order to perform this action, it is necessary to either use a
UpdatePanel
server-side control, which is what I ended up doing, as @weimar indicated and as this link shows https://www.codeproject.com/Articles/16945/ Simple-AJAX-File-UploadOr use MVC as @Lazarok shows in his answer https://stackoverflow.com/a/42734/24364
Body of the Index.cshtml file
UpLoad.cshtml file body
Body of the IndexController.cs file