0

I am getting this error while sendig the request from jQuery to CFC.

I am taking the file path from jquery and using that path I am loading the file and send it to database.

jQuery:

$("#submitbtn").click(function() {
  var fileInput = document.getElementById("fileupload");
  var filename = fileInput.files[0].name;
  var filepath = $("#fileupload").val();
  var filetype = fileInput.files[0].type;
  $.ajax({
    url: "_cfc/recommendation.cfc?method=evaluationattachment",
    type: "POST",
    data: {
      filepath: filepath,
      filename: filename,
      filetype: filetype
    },
  }).done(function(response) {
    console.log(response);

  }).fail(function(jqXHR, textStatus, errorMessage) {
    //window.location.href = "error.cfm";
  });

CFC:

<cffunction name="evaluationattachment" access="remote" returntype="any" returnformat="json">
  <cfargument name="filepath" type="string" required="no">
  <cfargument name="filename" type="string" required="no">
  <cfargument name="filetype" type="string" required="no">  
  <cfset dest = getTempDirectory()>
  <cffile action="upload" file="#filepath#" destination="#dest#" result="uploadResult" nameconflict="makeunique">
  <cfif uploadResult.fileWasSaved>
    <cfset theFile = uploadResult.serverDirectory & "/" & uploadResult.serverFile>
    <cffile action="readbinary" file="#theFile#" variable="attachment">
    <cfstoredproc procedure="recommendation.evaluationattachment">
      <cfprocparam cfsqltype="CF_SQL_BLOB" value="#attachment#" type="in">
      <cfprocparam cfsqltype="cf_sql_varchar" value="#filename#" type="in" maxlength="50">
      <cfprocparam cfsqltype="cf_sql_varchar" value="#filetype#" type="in" maxlength="12">
      <cfprocparam cfsqltype="cf_sql_varchar" value="#attachment_id#" type="out">
    </cfstoredproc>
    <cfset r = {"attachmentid"="#attachment_id#"}>
    <cfreturn r>
  </cfif>
</cffunction>
rrk
  • 14,861
  • 4
  • 25
  • 41
  • 2
    Try changing the `contentType` of your AJAX call to `multipart/form-data`. – Miguel-F Jun 04 '18 at 19:13
  • If I change this, now I am getting "The files upload action requires forms to use enctype="multipart/form-data". error.. –  Jun 04 '18 at 19:59
  • Correct, which is what I had you change it to. I assume you are getting this new error in some other part of your code. Correct? If so, that is a different question than this one. – Miguel-F Jun 04 '18 at 21:01
  • Try this. https://stackoverflow.com/a/21045034/1479535 – rrk Jun 05 '18 at 05:59

0 Answers0