除非页面至少回发了一次,否则FileUpload.PostedFile始终为null. [英] FileUpload.PostedFile is always null unless the page has posted back at least once?

查看:58
本文介绍了除非页面至少回发了一次,否则FileUpload.PostedFile始终为null.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎是一个显而易见的问题,但我仍然感到困惑.我在页面上也有一个ASP FileUpload控件,该控件也恰巧在UpdatePanel内部.我将UpdatePanel设置为UpdateMode ="Conditional".我还在触发器部分中设置了回发触发器(不是asyncpostback触发器).单击按钮后,应该将文件上传到数据库,由我的数据层处理.第一次尝试时,它会失败,因为PostedFile为null,但是您会看到页面执行回发(从本质上清除/重置下载控件).当我尝试再次上传文件时,就可以了.我首先想到的是因为它需要回发才能实际获取字节,对吗?好吧,因此在这种逻辑下,我认为如果尝试上载另一个文件,那么它实际上将上一个文件上载.它没有;它上传了新文件.这就是我感到困惑的地方.

Seems like an obvious question but I'm still confused. I have an asp FileUpload control on the page which also happens to be inside of an UpdatePanel. I have the UpdatePanel set to UpdateMode="Conditional". I also have a postback trigger (not an asyncpostback trigger) set in the triggers section. On button click, it is supposed to upload the file to a database, handled in my datalayer. On the first attempt, it will fail as the PostedFile is null, but you see the page perform a postback (which inherently clears/resets the download control). When I try uploading a file again, it is fine. My first thought was because it needed to postback in order to actually grab the bytes, right? Well ok, so under that logic, I figured if I attempted to upload a different file, then it would actually upload the previous file. It did not; it uploaded the new file. So that's where I'm confused.

以下是上传方法:

protected void ReplaceDoc()
{
    var data = ulReplaceDoc.FileBytes;
    var fileName = ulReplaceDoc.PostedFile.FileName;
    if (MocApi.ReplaceWorkingDocument(_request.MocRequestID, _currentUser, fileName, data))
    {
        //refresh files
        ShowFiles();
    }
}

挺直的.应该抓取文件,然后上传(在数据库上运行UPDATE查询以更改文件),然后刷新数据源以显示新文件.

Pretty straight forward. It is supposed to grab the file, upload it (which runs an UPDATE query on the database to change the file), then it refreshes a datasource to show the new file.

所以我的问题是:为什么它只会在第一次尝试时失败?还有,什么是达到此效果的更好方法?

So my question is this: Why does it only fail on the first try? Also, what would be a better way to achieve this effect?

推荐答案

在页面加载事件"中,添加以下代码,

In Page Load Event, add the following code,

Page.Form.Attributes.Add("enctype", "multipart/form-data");

它将起作用.第一次PostBack期间缺少enctype属性.

It will work. The enctype attribute is missing during first time PostBack.

这篇关于除非页面至少回发了一次,否则FileUpload.PostedFile始终为null.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆