Reputation: 121
I have a form:
<div id="form">
<form action="upload.cgi" method="get" enctype="multipart/form-data">
<input type="file" name="file" id="file"/>
<input type="submit" value="Upload"/>
</form>
</div>
and the upload.cgi is:
saveFile n =
do cont <- liftM fromJust $ getInputFPS "file"
let f = uploadDir ++ "/" ++ basename n
liftIO $ writeFile f cont
return $ paragraph << ("Saved")
uploadDir = "./uploadDirectory"
basename = reverse . takeWhile (`notElem` "/\\") . reverse
page t b = header << thetitle << t +++ body << b
myFromJust (Just a) = a
myFromJust Nothing = "gs"
cgiMain = do
mn <- getInputFilename "file"
h <- saveFile (myFromJust mn)
output . renderHtml $ page "Saved" h
main = runCGI $ handleErrors cgiMain
The problem is that every time I try to upload a file instead of saving that file, a file called gs is made(from myFromJust function, if I use fromJust I get Nothing and the program fails) whose contents is the uploaded file name.
So, I try to upload a file called something.zip and i get a file called gs whose contents is something.zip (written as text).
I think the problem is that the getInputFilename "file" or getInputFPS don't return anything but I dont't know why.
Is something wrong with the form or the program?
Upvotes: 1
Views: 328
Reputation: 1772
The problem is that the original code1 uses POST; but in the HTML Form of the question, HTML GET is used instead. The fix is to change GET
to POST
in the HTML.
From the original example where it uses HTTP POST:
fileForm =
form ! [method "post"
, enctype "multipart/form-data"]
<< [afile "file", submit "" "Upload"]
The new Form (partly below) where it improperly uses GET.
<form action="upload.cgi"
method="get" enctype="multipart/form-data">
Notes
1 See Section 9: Web/Literature/Practical web programming in Haskel
Upvotes: 1