ASP Classic reposting posted data

387 Views Asked by At

I am posting uploaded files to classic ASP using AJAX and I need to be able to repost the post stream to another site.

I can verify that the posted data from AJAX request is correct. I can read the files and save them on the local server. I can change the AJAX url to the "reposting" URL and it works fine.

The issue is simply reading the posted data and reposting.

let data = formData; //all the files we are uploading
$.ajax({
        url: `MyMultiFileHandler.asp`,
        cache: false,
        contentType: false,
        processData: false,
        enctype: 'multipart/form-data',
        data: data,
        type: `POST`,
    }).done(function(data) {
        console.log(`data: `, data);
    }).fail(function(xhr, textStatus, errorThrown) {
        console.log(xhr, textStatus, errorThrown);
    }).always(function() {
    });

MyMultiFileHandler.asp

dim postData: postData=request.Form

Set ServerXmlHttp = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")
ServerXmlHttp.open "POST", "https://example.com", false

'ServerXmlHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
ServerXmlHttp.setRequestHeader "Content-Type", "multipart/form-data"
ServerXmlHttp.setRequestHeader "Content-Length", Len(postData)

ServerXmlHttp.send postData

If ServerXmlHttp.status = 200 Then
    TextResponse = ServerXmlHttp.responseText
    response.write TextResponse ' Success! 0 files uploaded!
Else
    response.write err.description & "fault 6"
    response.end
End If

Set ServerXmlHttp = Nothing

EDITED to add the controller being called Here is the controller and function that's being called on the "repository" server:

    [HttpPost]
    //[ValidateAntiForgeryToken]
    public ActionResult Upload(string folderpath, IEnumerable<HttpPostedFileBase> imgfiles)
    {
        folderpath = !String.IsNullOrEmpty(folderpath) ? folderpath.Replace("|", "\\") : "";

        bool exists = System.IO.Directory.Exists(Server.MapPath($"/imagerepository/{folderpath}"));

        if(!exists)
            System.IO.Directory.CreateDirectory(Server.MapPath($"/imagerepository/{folderpath}"));
        int count = 0;
        var fileName = "";
        if (imgfiles != null)
        {
            foreach (var file in imgfiles)
            {
                if (file != null && file.ContentLength > 0)
                {
                    fileName = file.FileName;
                    var path = Path.Combine(Server.MapPath($"/imagerepository/{folderpath}"), fileName);
                    //return new JsonResult { Data = "Successfully " + count + " file(s) uploaded: " + fileName + " path " +path};
                    file.SaveAs(path);
                    count++;
                }
            }
        }
        return new JsonResult { Data = "Successfully upoaded " + count + " file(s)"};
    }
0

There are 0 best solutions below