我想实现我的耶索德应用大文件的上传。 现在,我有:
module Handler.File where
import Import
import System.Random
import System.FilePath
import Control.Monad
import qualified Data.ByteString.Lazy as LBS
import qualified Data.Text.Encoding
-- upload
uploadDirectory :: FilePath -- FIXME: make this configurable
uploadDirectory = "incoming"
randomFileName :: IO FilePath
randomFileName = do
fname'base <- replicateM 20 (randomRIO ('a','z'))
let fname = uploadDirectory </> fname'base <.> "bin"
return fname
fileUploadForm :: Form (FileInfo, Textarea)
fileUploadForm = renderDivs $ (,)
<$> fileAFormReq "Choose a file"
<*> areq textareaField "What's on the file?" Nothing
getFileNewR :: Handler RepHtml
getFileNewR = do
(formWidget, formEnctype) <- generateFormPost fileUploadForm
defaultLayout $ do
setTitle "Upload new file."
$(widgetFile "file-new")
postFileNewR :: Handler RepHtml
postFileNewR = do
user <- requireAuth
((result, formWidget), formEnctype) <- runFormPost fileUploadForm
case result of
FormSuccess (fi,info) -> do
fn <- liftIO randomFileName
liftIO (LBS.writeFile fn (fileContent fi))
let newFile = File (entityKey user) fn info (fileName fi) (fileContentType fi)
fid <- runDB $ insert newFile
redirect (FileViewR fid)
_ -> return ()
defaultLayout $ do
setTitle "Upload new file."
$(widgetFile "file-new")
这主要是很好,除了几个问题:
文件的最大尺寸约为2兆字节。 我有一个解决方法,但它这样做正确的方式? 我的修复是压倒maximumContentLength方法的默认实现耶索德情况下我的应用程序,就像这样:
maximumContentLength _(只是(FileNewR _))= 2 * 1024 * 1024 * 1024 - 2千兆字节maximumContentLength _ _ = 2 * 1024 * 1024 - 2兆字节
的存储器中所用的量等于一个文件的大小。 这实在是不理想的。 我想用tempFileBackEnd从http://hackage.haskell.org/packages/archive/wai-extra/1.2.0.4/doc/html/Network-Wai-Parse.html ,但我不知道如何实际连线,在我的请求,并使其与形式逻辑(隐藏_token场等)工作。
上传是“单发”:如何使其与基于Flash / HTML5上传,显示用户的工作正在进行中任何的例子吗?