How to upload file to a server with php - Livecode - livecode

I have problem with upload file to a server with php.
This is my code testForm.php
$uploaddir = 'uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
$sta["status"] = "Success";
echo stripslashes(json_encode($sta));
}
And in livecode
put the text of field "pF" into pathFile
put urlEncode("file:"& pathFile) into tFile
put libUrlFormData("userfile",tFile) into tArgList
post tArgList to URL "http://localhost:9999/livecodeTestJson/testForm.php"
It's not working.

I do not know, if your php code is correct, but your livecode part isn´t.
To post files to a webserver you need the libUrlMultipartFormData function.
Below you find a sample from the libURL documentatin. I adjusted it a little bit using your values
put empty into tFormData
put "http://www.someserver.com/cgi-bin/form.cgi" into tUrl
put the text of field "pF" into pathFile
put "<file>" & pathfile into tFile
if libUrlMultipartFormData(tFormData, "userfile", tFile) is not empty then
answer it ##error
else
set the httpHeaders to line 1 of tFormData
post line 2 to -1 of tFormData to url tUrl
## check the result, etc., here
set the httpHeaders to empty
end if
If you also want post form data then you have to include that data in the call of the libURLMultiPartFormData.
Let´s say you have the form fields "name", "email" and "message" and the variables tName, tEmail and tMessage contain the data then you would replace line 5 of the sample script with the following line
if libUrlMultipartFormData(tFormData, "name", tName, "email", tEmail, "message", tMessage, "userfile", tFile) is not empty

Related

Find and replace header/footer text in Word with pywin32

I hope everyone reading this is well. My problem is as follows:
I am trying to find a way to find and replace text in the header (or footer) of a Word document (docx), using the pywin32 library.
Here's what I've tried...
I've only made it as far as replacing the header (or footer) text in its entirety. Here's the code for that.
import win32com.client
word_app = win32com.client.DispatchEx('Word.Application')
word_app.Visible = False
word_app.DisplayAlerts = False
word_app.Documents.Open(str('sourcefile.docx'))
'''
For this example, sourcefile.docx has three pages; and it is
configured to have a different header for the first page,
hence Headers(2) and Headers(1) below.
'''
word_app.ActiveDocument.Sections(1).Headers(2).Range.Text = "Page 1 header text"
word_app.ActiveDocument.Sections(1).Headers(1).Range.Text = "Subsequent pages header text"
word_app.ActiveDocument.SaveAs(str(outputfile))
word_app.ActiveDocument.Close(SaveChanges=False)
word_app.Application.Quit()
What I want to do is have templated text in the header; e.g., %NAME% Agreement, and replace only the variable portions. In this example, it's only the %NAME% part. At present, I have only managed to accomplish a total replacement of the entirety of the header's content. I'd like to be more surgical. Any help is greatly appreciated.

How to convert a URL within a field into a photo?

In Livecode I am pulling information from a database using XML and organizing it using a repeat function. Therefore, for each node there is information such as photo, name, age, etc.
The photo, however, is read from the XML as the URL link of the photo. Is there a way to automatically load this URL and make the picture appear with the information also in this node following it?
on preOpenStack
put url "http://www.petango.com/webservices/wsadoption.asmx/AdoptableSearch?authkey=XXXXXXXX&speciesID=&sex=&ageGroup=&location=&site=&onHold=&orderBy=&primaryBreed=&secondaryBreed=&specialNeeds=&noDogs=&noCats=&noKids=&stageID=" into tURL
put revCreateXMLTree( tURL, true, true, false) into tInfo
put revXMLChildNames( tInfo, "ArrayOfXmlNode", return, "XmlNode", true) into tChildren
repeat for each line tChild in tChildren
add 1 to x
put revXMLChildNames( tInfo, "ArrayOfXmlNode/"&tChild&backslash, return, "adoptableSearch", true) into tAdoptable
put revXMLNodeContents( tInfo, "ArrayOfXmlNode/"&tChild&"/"&tAdoptable&"/Photo") into tData
put "Name: " & revXMLNodeContents( tInfo, "ArrayOfXmlNode/"&tChild&"/"&tAdoptable&"/Name") & return after tData
put return after tData
put return after tData
end repeat
put tData & return after tOutput
set the text of field "tData" to tOutput
end preOpenStack
You write that an XML node returns the URL to the photo, but you don't write which node this is. assume that
revXMLNodeContents( tInfo, "ArrayOfXmlNode/"&tChild&"/"&tAdoptable&"/Photo")
returns the URL. Put the URL into a variable and use it to set the text of an image object:
put revXMLNodeContents( tInfo, "ArrayOfXmlNode/"&tChild&"/"&tAdoptable&"/Photo") into myUrl
put url myUrl into myPictureData
if the result is empty then
set the text of img 1 to myPictureData
else
beep
answer error "Can't load picture."
end if
Sorry for hogging the thread after Marks excellent answer. But I usually just set the filename for the image:
repeat for each line tChild in tChildren
add 1 to x
...
put revXMLNodeContents( tInfo, "ArrayOfXmlNode/"&tChild&"/"&tAdoptable&"/Photo") into myUrl
create image ("image" && x)
put it into tImageID
set the filename of tImageID to myURL
...
end repeat
This will not check if the image loaded correctly but as you will not get any image anyway it is a easy solution.

Linux application in livecode

Spell Check is a default application in Linux. With the help of that application, can we check the spelling of a text field while users enter data?
Some (or many?) Linux distributions contain a command line utility that is called spell. If you run this with words as parameters, you need to press return a second time, but if you use a file as a paramater, you don't need to press return again. This means that a solution could be:
write the text of a field to a file
run the command line utility from LiveCode's shell function with the file as parameter
parse the result returned by the shell function
Before you try this, open your terminal on Linux and type spell. Press enter to see if the command is recognised. If yes, then the script below should work.
This script writes the text of a field to a file, does a spell check on the file and returns the incorrect words to LiveCode. I haven't tested the script and you may have to tweak it a little.
function spellCheck theText
// works on Linux only
if the platform is "Linux" then
// remove everything that isn't a word
put replaceText(theText,"[^\w]","") into myWords
// write clean data to a temporary file
put the tempName into myTempFile
put myWords into url ("file:" & myTempFile)
// call spell with shell
put "spell" && myTempFile into myShell
// only return the incorrect words
put line 2 to -1 of shell(myShell) into myCorrections
// return the incorrect words to calling handler
return myCorrections
else
// this isn't Linux
return "error"
end if
end spellCheck
//theField is the short name of a field
on checkField theField
// call above function
put spellCheck(the text of fld theField) into myWords
// myWords should now contain the incorrect words
if myWords is not "error" then
lock screen
// parse incorrect words and mark them in the field
repeat with x = 1 to number of words of field theField
if myWord is among the lines of myWords then
// an incorrect word has been found and is marked red
set the textColor of word x of fld theField to red
end if
end repeat
unlock screen
end if
end checkField
Usage: checkField shortNameOfTheField

Adding a newline character within a cell (CSV)

I would like to import product descriptions that need to be logically broken according by things like description, dimensions, finishes etc. How can I insert a line break so that when I import the file they will show up?
This question was answered well at Can you encode CR/LF in into CSV files?.
Consider also reverse engineering multiple lines in Excel. To embed a newline in an Excel cell, press Alt+Enter. Then save the file as a .csv. You'll see that the double-quotes start on one line and each new line in the file is considered an embedded newline in the cell.
I struggled with this as well but heres the solution. If you add " before and at the end of the csv string you are trying to display, it will consolidate them into 1 cell while honoring new line.
csvString += "\""+"Date Generated: \n" ;
csvString += "Doctor: " + "\n"+"\"" + "\n";
I have the same issue, when I try to export the content of email to csv and still keep it break line when importing to excel.
I export the conent as this: ="Line 1"&CHAR(10)&"Line 2"
When I import it to excel(google), excel understand it as string. It still not break new line.
We need to trigger excel to treat it as formula by:
Format -> Number | Scientific.
This is not the good way but it resolve my issue.
supposing you have a text variable containing:
const text = 'wonderful text with \n newline'
the newline in the csv file is correctly interpreted having enclosed the string with double quotes and spaces
'" ' + text + ' "'
On Excel for Mac 2011, the newline had to be a \r instead of an \n
So
"\"first line\rsecond line\""
would show up as a cell with 2 lines
I was concatenating the variable and adding multiple items in same row. so below code work for me. "\n" new line code is mandatory to add first and last of each line if you will add it on last only it will append last 1-2 character to new lines.
$itemCode = '';
foreach($returnData['repairdetail'] as $checkkey=>$repairDetailData){
if($checkkey >0){
$itemCode .= "\n".trim(#$repairDetailData['ItemMaster']->Item_Code)."\n";
}else{
$itemCode .= "\n".trim(#$repairDetailData['ItemMaster']->Item_Code)."\n";
}
$repairDetaile[]= array(
$itemCode,
)
}
// pass all array to here
foreach ($repairDetaile as $csvData) {
fputcsv($csv_file,$csvData,',','"');
}
fclose($csv_file);
I converted a pandas DataFrame to a csv string using DataFrame.to_csv() and then I looked at the results. It included \r\n as the end of line character(s). I suggest inserting these into your csv string as your row separation.
Depending on the tools used to generate the csv string you may need escape the \ character (\r\n).

ASP FileObject.Name converting to '?'

I have a script that lists all files in a directory, then for each one it will Response.Write the name and how many downloads it has.
I have everything completed, but when I went for a test, the files that have "odd" characters in the name are replace with a ?
I'm guessing, that since some files have foreign languages as there name, and that some have the iPhone emoji icons in the name, that it doesn't recognize it and puts a ? instead, but this is a serious issue since I can't give the correct file name back to the user, then that incorrect name is fed back into the url to download. (Which doesn't work)
Any suggestions?
Edit:
set fs=Server.CreateObject("Scripting.FileSystemObject")
set fo=fs.GetFolder(Server.MapPath("."))
for each file in fo.files
if fs.GetExtensionName(file.Path) = "plist" then
dim tempList, tempName, ...
tempList = split(file.Name, ".")
'Manipulate name and data ...
Response.write(name)
end if
next
The file names themselves have odd characters, and file.Name returns a ? instead of what is actually there.
18アイコン is one example.
Here's some code which works fine for me:
<%# Language="VBScript" CodePage="65001" %><%
Option Explicit
Response.CodePage = 65001
Response.CharSet = "utf-8"
Dim fs, fo, file
Set fs = Server.CreateObject("Scripting.FileSystemObject")
Set fo = fs.GetFolder(Server.MapPath("."))
For Each file In fo.files
If fs.GetExtensionName(file.Path) = "plist" Then
' Do whatever here...
Response.Write file.Name & "<br>"
End If
Next
%>
If you are using any variables that you didn't dimension beforehand, you'll need to remove the Option Explicit; otherwise, VBScript will complain that you didn't dimension them.
Edit: I copy & pasted the wrong code; this code works.

Resources