in Education by
I have a form with a File Input and one Button, when I press the button the file should go to the server side. When I send a file to the server the ajax response is success, never stop in the c# webmethod breakpoint that I use. What I am doing wrong? The Form: (Default.aspx)
Seleccione el archivo que desea subir
The Script: (Default.aspx) function sendFile() { var data = new FormData(); var file = $("#FileInput")[0].files[0]; data.append("name", file.name); data.append("size", file.size); data.append("type", file.type); data.append("file", file); $.ajax({ type: "POST", async: true, url: "Default.aspx/UploadBlock", data: data, cache: false, contentType: false, processData: false, success: function (result) { alert("Success: " + result); }, error: function (xhr, status) { alert("An error occurred: " + status); } }); }; The WebMethod: (Default.aspx.cs) [WebMethod(EnableSession = true)] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public static Respuesta UploadBlock() { Respuesta res = new Respuesta { Success = true, Message = "OK" }; //Break point here return res; } Thanks. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
In case someone comes across this as I did... WebMethods expect a content-type of application/json - https://stackoverflow.com/a/25531233/2913189 If you set the content-type to false, the ajax call will not hit your webmethod, it will go to page_load. There seems to be some other ways of accomplishing a file upload via stringifying the file but I was unable to get a working solution, so I simply created an HttpHandler (.ashx) file, compiled, and added the reference in web.config. Using a handler, you can set the content-type to "false" in your ajax call and not have any problems. I sent the information as FormData, and it was easily consumed in the handler using context.Request.Files and context.Request snippet of ajax call: var fileControl = $("#file")[0].files[0]; var formData = new FormData(); formData.append("employeeId", employeeId); formData.append("userfile", fileControl); formData.append("filetype", uploadTypeSelect.val()); $.ajax({ type: "POST", contentType: false, url: "/Handlers/MyUploadHandler.ashx", processData: false, data: formData, success: function (msg) { //do something }, error: function (xhr, ajaxOptions, thrownError) { //do something } }); Snippet of handler: public override async Task ProcessRequestAsync(HttpContext context) { context.Response.ContentType = "text/plain"; var uploadedFile = context.Request.Files[0]; //only uploading one file var fileName = uploadedFile.FileName; var fileExtension = uploadedFile.ContentType; var folder = "MyOneDriveFolder"; //this is an method written elsewhere to upload a file to OneDrive var uploaded = await OneDriveUpload.UploadDocument(filename,uploadedFile.InputStream, folderName, 0); context.Response.Write("Whatever you like"); }

Related questions

0 votes
    in my app i want to upload an image chosen via UIImagePickerController to a database which only accepts JPEG ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jan 16, 2022 in Education by JackTerrance
0 votes
    I'm coding a basic portfolio page in ASP.NET Core Razor Pages. I want to have a contact form at ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 9, 2022 in Education by JackTerrance
0 votes
    Asynchronous Javascript and XML (Ajax) is technique for creating better, faster, and more interactive web applications. In addition to ... Perl, and C++ D. Java, ASP, and C#...
asked Mar 10, 2023 in Technology by JackTerrance
0 votes
    When working with Ajax applications, which is faster, XML or JSON? A. XML, because it is extensible B. JSON, ... D. JSON, because it is already parsed into a JavaScript object...
asked Mar 10, 2023 in Technology by JackTerrance
0 votes
    I have an html select on my page $query = mysql_query("select * from results"); echo " "; while ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 11, 2022 in Education by JackTerrance
0 votes
    I have an html select on my page $query = mysql_query("select * from results"); echo " "; while ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 6, 2022 in Education by JackTerrance
0 votes
    I have succeed implement this code to remove product from cart with Ajax. But it didn't works with ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
0 votes
    I have following code and I want to get data from service. I have set everything from what I get ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    I have an html select on my page $query = mysql_query("select * from results"); echo " "; while ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    When I insert the details in form and click on insert button, it shows "User has been added successfully ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 16, 2022 in Education by JackTerrance
0 votes
    I have following code and I want to get data from service. I have set everything from what I get ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 12, 2022 in Education by JackTerrance
0 votes
    The following code from Bacon.js will push the data object returned from the AJAX request into the console. ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 10, 2022 in Education by JackTerrance
0 votes
    When I insert the details in form and click on insert button, it shows "User has been added successfully ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 10, 2022 in Education by JackTerrance
0 votes
    When I insert the details in form and click on insert button, it shows "User has been added successfully ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 8, 2022 in Education by JackTerrance
0 votes
    I found this example on the official site of DialogFlow using Node.js and it is working fine, but ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 1, 2022 in Education by JackTerrance
...