在 C# 中从 POST 中查找文件数据

发布于 2024-07-29 03:49:19 字数 1062 浏览 4 评论 0原文

这可能是我所遗漏的显而易见的事情。 帮助我,所以! 我正在尝试从 Web 表单(不是内置的 C# 表单)访问通过 POST 提交的文件数据。我不知道如何执行此操作,并且 MSDN 在这方面毫无帮助。

这是我迄今为止尝试过的三件事:

Request["file"];
Request.Form["file"];
Request;

还有什么? 这似乎只是缺少 POST 数据。 如果有帮助,这是 firebug 的输出:

Content-Type: multipart/form-data; boundary=---------------------------149243018821763
Content-Length: 703
-----------------------------149243018821763
Content-Disposition: form-data; name="file"; filename="testsearch.txt"
Content-Type: text/plain
Just some plain text data.
-----------------------------149243018821763
Content-Disposition: form-data; name="folder"
ftp://wwwdev.jbu.edu/athletics/resource/media/testsearch.txt
-----------------------------149243018821763
Content-Disposition: form-data; name="MAX_FILE_SIZE"
100000
-----------------------------149243018821763
Content-Disposition: form-data; name="u"
username
-----------------------------149243018821763
Content-Disposition: form-data; name="p"
password
-----------------------------149243018821763--

This is probably something blatantly obvious that I'm just missing. Help me, SO! I'm trying to access file data submitted via POST from a web form (not the built in C# ones though.) I have no idea how to do this, and MSDN is singularly unhelpful in this matter.

Here's the three things I've tried so far:

Request["file"];
Request.Form["file"];
Request;

What else is there? It just seems to be a missing POST data. If it helps, here's the output from firebug:

Content-Type: multipart/form-data; boundary=---------------------------149243018821763
Content-Length: 703
-----------------------------149243018821763
Content-Disposition: form-data; name="file"; filename="testsearch.txt"
Content-Type: text/plain
Just some plain text data.
-----------------------------149243018821763
Content-Disposition: form-data; name="folder"
ftp://wwwdev.jbu.edu/athletics/resource/media/testsearch.txt
-----------------------------149243018821763
Content-Disposition: form-data; name="MAX_FILE_SIZE"
100000
-----------------------------149243018821763
Content-Disposition: form-data; name="u"
username
-----------------------------149243018821763
Content-Disposition: form-data; name="p"
password
-----------------------------149243018821763--

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陈年往事 2024-08-05 03:49:19

尝试...

Request.Files["file"]

Reflector 显示文件已从 Request.Form 集合中排除。

Try...

Request.Files["file"]

Reflector shows that files are excluded from the Request.Form collection.

み格子的夏天 2024-08-05 03:49:19

我假设您正在使用 FileUpload 控件...您需要在 Page_Load 中添加类似的内容。

if (FileUpload1.HasFile)
            {
                if (System.IO.Path.GetExtension(FileUpload1.FileName).ToLower() == ".jpg")
                {
                    fileOK = true;
                }
                if (fileOK)
                {
                    try
                    {
                        FileUpload1.PostedFile.SaveAs(System.IO.Path.Combine(path,  newFileName + ".jpg"));

I'm assuming you're using a FileUpload control.... You need to put something like this in your Page_Load.

if (FileUpload1.HasFile)
            {
                if (System.IO.Path.GetExtension(FileUpload1.FileName).ToLower() == ".jpg")
                {
                    fileOK = true;
                }
                if (fileOK)
                {
                    try
                    {
                        FileUpload1.PostedFile.SaveAs(System.IO.Path.Combine(path,  newFileName + ".jpg"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文