0

I have a web page which sends a video file/blob to another web page in the using FormData using POST method and XMLHttpRequest. I want to return the response as string from the Posted URL in calling function. i.e after successfull upload file i want to return string to the caller.

index.aspx:

function SendFile(blob)
{
var file = new File([blob], "testfile123.webm");
var oData = new FormData();
oData.append("file", file,"testfile.webm");
var oReq = new XMLHttpRequest();
oReq.open("POST", "upload.aspx", true);
oReq.onload = function (oEvent) 
{
  if (oReq.status == 200) 
  {
    alert("Uploaded"+oReq.responseText);
  } 
  else {
    alert("Error");                     
  }
};
oReq.send(oData);
}

Upload.aspx:

protected void Page_Load(object sender, EventArgs e)
{
     string folderPath = GetUploadFolderPath();
     string filename = DateTime.Now.ToString("dd-MM-yyyy-HH-mm-ss") + ".webm";  
     HttpFileCollection fileCollection = Request.Files;
     for (int i = 0; i < fileCollection.Count; i++)
     {
         HttpPostedFile postedFile = fileCollection[i];
         var filePath = folderPath + filename; //postedFile.FileName;
         postedFile.SaveAs(filePath);

     }             
 }

In above code the file is created at specified location all works fine.I know its not possible in Page_Load event. please suggest correct method to send responsetext/string after file upload.

Help Appreciated.

SHEKHAR SHETE
  • 5,964
  • 15
  • 85
  • 143
  • Not really the issue here, but I recommend to use generic handlers **ashx** for data transfers instead of **aspx**. Usually one does not need the full life cycle (init, load, render, prerender, onerror) and webforms provided by **aspx** for fileuploads. There are examples on [SO](https://stackoverflow.com/questions/8466941/upload-file-using-jquery-and-handlerashx) – Lain Jul 16 '20 at 07:59
  • Just guessing here, but isn't it simply something like `Response.Write("some response");`? –  Jul 16 '20 at 08:06
  • @Lain Request.Files is not accessible in static method. :( also i need the response as text/string and do not want to write the response using Response.Write – SHEKHAR SHETE Jul 16 '20 at 08:09

1 Answers1

1

This is a scraped together example on how to target specific functions inside aspx with jquery.ajax using vb.net. Sadly I am not fluent in C#.

Note that if you are using an ancient version of .net (two or lower), you have to add System.Web.Extensions in your web.config to make it work. Depending on the IIS either as httpModules or as modules.

AJAX.aspx

<WebMethod(EnableSession:=True)> _
Public Shared Function abc(def As String) As Boolean
    ''REM: Do stuff
    Return True
End Function

xhr call using jquery

$.ajax({
    type: "POST",
    url: "AJAX.aspx/abc",
    data: "{'def':'test'}",
    contentType: "application/json; charset=utf-8",
    dataType: "text",
    success: function(response){...},
    error: function(jqXHR, textStatus, errorThrown){...}
});

Yet, I still recommend to use generic handlers, like also shown here. It is more clean, slim and offers more control on what you actually want to do. Also the System.Web.Extensions are not required.

Lain
  • 3,657
  • 1
  • 20
  • 27