Coldfusion Search HTML <textarea> Then Covert Base64 String To File On Server

岁酱吖の 提交于 2019-12-08 10:34:22

问题


EDIT: Replace base64 string that is in the textarea with a URL. The textarea is a WYSIWYG editor (CKEditor). I need to upload an image to the server file system. I'm trying to use this code to convert the string to an actual image and then in the textarea replacing the base64 string with the location of the image on the server (URL).

<cfset image = imageReadBase64(#LocalOccurrence#)>
<cfimage source="#image#" 
         destination="#save_image_to_this_location# 
         & #name_of_image#  
         & #extension_of_image#" 
         action="write"> 

Original Question: Using ColdFusion, am trying to find all base64 image strings inside HTML then save each as its own file on the server, create URL, and insert into database. I need help crafting a loop at this point.

I got as far finding a single occurrence of the base64 string with this code:

<cfset textarea_to_search = #form.overview_text#>
<cfset string_base64_header = "base64,">
<cfset string_base64_ending = '"'>

<cfoutput>
  <cfset mystart = find(#string_base64_header#, #textarea_to_search#)>
  <cfset myend = find(#string_base64_ending#,#textarea_to_search#,#mystart#)>
  <cfset my64 = mid(#textarea_to_search#, (#mystart#+7), ((#myend#-7)-#mystart#))>
  <span style=font-size:8px;"> #mystart#, #myend#, #my64#</span>
</cfoutput>

Re-wrote the original loop to look like this but it only returns the first occurrence of the base64 string:

<cfset counter = 1>
<cfset my_array =[]>
<cfoutput>
  <cfloop condition = "counter LTE 5">
    <cfset mystart = find(#string_base64_header#, #textarea_to_search#)>
    <cfset myend = find(#string_base64_ending#,#textarea_to_search#,#mystart#)>
    <cfset my64 = mid(#textarea_to_search#, (#mystart#+7), ((#myend#-7)-#mystart#))>
    <span style=font-size:8px;"> #mystart#, #myend#, #my64#</span>
    <cfset ArrayAppend(my_array, #my64#)>
    <cfset counter = counter+1>
  </cfloop>
  <cfdump var = "#my_array#">

</cfoutput>

回答1:


There are a number of ways you could go about this. Possibly with a regex would be best, though I couldn't get an easy example of this working. Alternatively, you could replace the occurrences in the string as you find them and keep looking until there are none left.

You'd have to do more work than this for error checking / validation, etc, but here's a basic example. Here's the full example.

<cfset Base64Header = "base64,">
<cfset Base64Ending = '"'>

<cfset ResultsArray =[]>
<cfset ContinueSearching = true>

  <cfloop condition = " ContinueSearching eq true "><cfoutput>

    <cfset StartingIndex = find( Base64Header, SearchText)>

    <cfif StartingIndex eq 0>
        <cfset ContinueSearching = false>
        <cfcontinue/>
    </cfif>

    <cfset EndingIndex = find( Base64Ending , SearchText, StartingIndex )>

    <cfset FullOccurrence =  mid(#SearchText#, (#StartingIndex#), ((#EndingIndex#)-#StartingIndex#))>
    <cfset LocalOccurrence = mid(#SearchText#, (#StartingIndex#+7), ((#EndingIndex#-7)-#StartingIndex#))>

    <cfset ArrayAppend(ResultsArray, #LocalOccurrence#)>

    <cfset SearchText = replace(SearchText, FullOccurrence, "")>

    <cfset StartingIndex = 0>

  </cfoutput></cfloop>

  <cfdump var = "#ResultsArray#">


来源:https://stackoverflow.com/questions/46001839/coldfusion-search-html-textarea-then-covert-base64-string-to-file-on-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!