gpt4 book ai didi

file-upload - 在 Racket 中,你如何允许文件上传到网络服务器?

转载 作者:行者123 更新时间:2023-12-05 03:15:07 25 4
gpt4 key购买 nike

我正在尝试使用 Racket 的文件上传 formlet ( http://docs.racket-lang.org/web-server/formlets.html ) 启用文件上传到网络服务器。问题在于 formlet-process 只返回文件名而不是其内容。

这是我目前所拥有的:

#lang web-server/insta
(require web-server/formlets
web-server/http
xml)

; start: request -> doesn't return
(define (start request)
(show-page request))

; show-page: request -> doesn't return
(define (show-page request)
; Response generator
(define (response-generator embed/url)
(response/xexpr
`(html
(head (title "File upload example"))
(body (h1 "File upload example"))
(form
([action ,(embed/url upload-handler)])
,@(formlet-display file-upload-formlet)
(input ([type "submit"] [value "Upload"]))))))

(define (upload-handler request)
(define a-file (formlet-process file-upload-formlet request))
(display a-file)
(response/xexpr
`(html
(head (title "File Uploaded"))
(body (h1 "File uploaded")
(p "Some text here to say file has been uploaded")))))

(send/suspend/dispatch response-generator))


; file-upload-formlet: formlet (binding?)
(define file-upload-formlet
(formlet
(div ,{(required (file-upload)) . => . a-file})
a-file))

在这种情况下,a-file 被设置为带有文件名的字节字符串,而不是文件的内容。如何获取文件的内容,以便将其写入服务器上的文件?

预先感谢您的帮助!

最佳答案

好吧,这里有一些有用的东西,但我不确定这是最好的做事方式。基本上我必须

  1. 将 method="POST"和 enctype="multipart/form-data"添加到表单字段 html(是的,忽略这些的小学生错误,但我是新手)
  2. 使用 binding:file-filename 和 binding:file-contents 从文件上传 formlet 返回的绑定(bind)中提取文件名和内容。

有助于解决这个问题的引用资料是 http://lists.racket-lang.org/users/archive/2009-August/034925.htmlhttp://docs.racket-lang.org/web-server/http.html

这是工作代码。显然 WORKINGDIR 需要设置为一些工作路径。

#lang web-server/insta
(require web-server/formlets)

; start: request -> doesn't return
(define (start request)
(show-page request))

; show-page: request -> doesn't return
(define (show-page request)
; Response generator
(define (response-generator embed/url)
(response/xexpr
`(html
(head (title "File upload example"))
(body (h1 "File upload example"))
(form
([action ,(embed/url upload-handler)]
[method "POST"]
[enctype "multipart/form-data"])
,@(formlet-display file-upload-formlet)
(input ([type "submit"] [value "Upload"]))))))

(define (upload-handler request)
(define-values (fname fcontents)
(formlet-process file-upload-formlet request))
(define save-name (string-append "!uploaded-" fname))
(current-directory WORKINGDIR)
(display-to-file fcontents save-name #:exists 'replace)
(response/xexpr
`(html
(head (title "File Uploaded"))
(body (h2 "File uploaded")
(p ,fname)
(h2 "File size (bytes)")
(p ,(number->string (file-size save-name)))))))

(send/suspend/dispatch response-generator))


; file-upload-formlet: formlet (binding?)
(define file-upload-formlet
(formlet
(div ,{(file-upload) . => . binds})
; (formlet-process file-upload-formlet request)
; returns the file name and contents:
(let
([fname (bytes->string/utf-8 (binding:file-filename binds))]
[fcontents (binding:file-content binds)])
(values fname fcontents))))

关于file-upload - 在 Racket 中,你如何允许文件上传到网络服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20588641/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com