I am working on a web page
that needs to send a video file/blob
to another web page using FormData
and the POST
method with XMLHttpRequest
. My goal is to receive a response as a string from the called URL in the calling function. Essentially, after successfully uploading the file, I want to return a 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.SaveAs(filePath);
}
}
In the above code, the file is created at the specified location and everything works correctly. I understand that it is not possible in the Page_Load
event. Can someone suggest the correct method to send a response text/string after the file upload?
Your help is greatly appreciated.