1
votes

I've been tasked with adding file uploading to a web based application that is using Classic ASP. Unfortunately I am not allowed to use any other technology, and was told it is what it is so suggesting anything but ASP won't help in this instance.

A form POST with $.ajax is called upon submitting a simple form with some input fields and one file field, that points to a Classic ASP file that handles recordset inserts via request.form. I have added contentType: false in hopes to enctype="multipart/form-data" but been getting the following error:

Object doesn't support this property or method: 'Binary' because I changed the request.form in the insert posted below, but it won't work with request.form either. With request.form I get this error: [Microsoft][ODBC Microsoft Access Driver] Data type mismatch in criteria expression.

  $.ajax({
type   : 'POST',
url    : 'manipulateData.asp?sqlType='+newdbTable+'&sqlRecordId='+recordId+'',
data   : formData,
    encode : true,
contentType : false,
success: function(data) {
    // do something with data
},
error: function(data) {
    console.log("ERROR" + JSON.stringify(data));
}

})

Inside the Classic ASP file manipulateData.asp is a connection string that is called inside a case statement and beneath that is more Classic ASP file for a single file upload shown below.

I was trying to implement Shadow Wizards solution but wanted to try this first as I was trying to keep things simple.

 cn.execute("INSERT INTO CONTENT (contentTitle, contentContent, contentDate, categoryId, userLevel) VALUES ('" & Request.Form("contentTitle") & "','" & Request.Form("contentContent") & "','" & Request.Form("contentDate") & "','" & Request.Form("categoryTitle") & "','" & Request.Form("userLevel") & "')")

' TEST FILE UPLOAD
 Dim objUpload 
 Dim strFile, strPath
 ' Instantiate Upload Class '
 Set objUpload = New clsUpload
 strFile = objUpload.Fields("file").FileName
 strPath = server.mappath("/files") & "/" & strFile
 ' Save the binary data to the file system '
 objUpload("file").SaveAs strPath
 Set objUpload = Nothing

Is what I am attempting to do even possible as I've ready that request.form and request.binary can't be used at the same time? If not then what would be some alternatives?

1

1 Answers

1
votes

I do not like this answer for the record but calling another function on success to then get the path of the file then passing that in another ajax call to a Classic ASP file to upload worked.

  $.ajax({
    type   : 'POST',
    url    : 'manipulateData.asp?sqlType='+newdbTable+'&sqlRecordId='+recordId+'',
    data   : formData,
    encode : true,
    contentType : false,
    success: function(data) {
    // file upload function
      uploadFile()
    },
    error: function(data) {
      console.log("ERROR" + JSON.stringify(data));
     }
    })

And the upload ajax

function uploadFile(formData) {
var data = new FormData();
$.each($('#file1')[0].files, function(i, file) {
    data.append('file-'+i, file);
});


  $.ajax({
            type        : 'POST',
             url         : 'uploadFileTest.asp',
            data        : data,
            async: true,
            cache: false,
            contentType: false,
            processData: false,
            success: function(data) {
                // do something else here or not
            },
            error: function(data) {
                // SHOW ERROR
                console.log("ERROR" + JSON.stringify(data));
            }
        })
} 

I would still very much like to know if I could eliminate the second function call or if this is the nature of how Classic ASP handles FORM and BINARY posts, that they must be separate?