2014-04-29 4 views
2

CSV 파일을 업로드하고 DB로 저장 될 테이블에 구문 분석하려고합니다. Yesod의 파일 업로드 예제로 놀고 있었지만 최신 버전의 Yesod에서 작동하지는 못합니다. Yesod 1.2.5.2 and GHC 7.6.3 on Ubuntu 14.04을 사용하고 있습니다. 나는 ByteString가 있으면, 나는 그것을 구문 분석 Data.Csv을 사용하는 것입니다업로드 된 파일의 내용을 읽는 방법

postUploadTransactionR :: Handler Html 
postUploadTransactionR = do 
    ((result, widget), enctype) <- runFormPost fileUploadForm 
    case result of 
    FormSuccess (account, fi) -> do 
        -- ??? I would like to get the contents of fi and send it to a CSV parser. 
       -- (fileSourceRaw fi)??? 

       redirect (HomeR) 
    _ -> return() 

    defaultLayout $ do 
     $(widgetFile "upload_transactions") 

: 아래

이 내가 당신의 도움을받을하고자하는 부분입니다 내 코드

fileUploadForm :: Form ((Key Account), FileInfo) 
fileUploadForm = renderDivs $ (,) 
    <$> areq (selectField accounts) "Account" Nothing 
    <*> fileAFormReq "Choose a file" 
    where 
    accounts = do 
     entities <- runDB $ selectList [] [Asc AccountName] 
     optionsPairs $ map (\s -> (accountName $ entityVal s, entityKey s)) entities 

getUploadTransactionR :: Handler Html 
getUploadTransactionR = do 
    (widget, enctype) <- generateFormPost fileUploadForm 
    defaultLayout $ do 
     setTitle "Upload new file." 
     $(widgetFile "upload_transactions") 

입니다 예 : decode NoHeader s :: Either String (Vector (Vector ByteString))

업로드 된 파일에서 파일 내용을 가져올 수있는 사람이 알려주시겠습니까? 디스크에 파일을 저장할 필요가 없습니다.

감사합니다.

postSomethingR = do 
    ((res, _), _) <- runFormPost form 
    case res of 
    FormSuccess (account, file) -> do 
     bytes <- runResourceT $ fileSource file $$ sinkLbs 
     -- Parse the ByteString in another thread 
     parseHandler <- handlerToIO 
     liftIO $ forkIO $ parseHandler $ do 
     case CSV.parseCSV csvSettings (decodeUtf8 . toStrict $ bytes) of 
      Left err -> ... 
      Right vector -> runDB $ do ... 

미안 해요, 난 휴대폰에서이 게시하도록하겠습니다 :

답변

3

이 내가 할 것입니다.

+0

감사합니다. 나는'liftIO $ print bytes'를 할 수 있었고 잘 작동했습니다. ByteString이있는 지금 CSV 파싱을 추가하려고합니다. – Ecognium

관련 문제