文件上传无效 [英] file upload not working

查看:87
本文介绍了文件上传无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在处理文件上传,但我不知道为什么它没有拿任何文件。



i已经在asp.net页面添加了fileupload和上传按钮,



i选择一个文件n点击上传按钮,在按钮上我得到了fileone .hasfile为空。



我的代码:



  protected   void  btnupload_Click( object  sender,EventArgs e) 
{
if (PhotoUpload1.HasFile)
{
Session [ ImageBytes] = PhotoUpload1.FileBytes;
string str = Server.MapPath(PhotoUpload1.FileName);
ImagePreview.ImageUrl = 〜/ DriverSetup / + PhotoUpload1.FileName;
}

}





它不会进入if循环,只返回false;



plz建议我,



更新面板代码:



< pre lang =xml>< asp:UpdatePanel ID =UpdatePanel1runat =server> 
< ContentTemplate>
< asp:Image CssClass =img img-thumbnailWidth =150pxHeight =180pxID =ImagePreviewrunat =server/>
< br />
< asp:FileUpload ID =PhotoUpload1runat =server/>
< br />
< asp:Button ID =btnuploadrunat =serverText =Upload
onclick =btnupload_Click/>
< asp:UpdateProgress ID =UpdateProgress1runat =server>
< ProgressTemplate>请稍候...< / ProgressTemplate>
< / asp:UpdateProgress>
< / ContentTemplate>
<触发器>
< asp:PostBackTrigger ControlID =btnupload/>
< /触发器>
< / asp:UpdatePanel>

< / pre>





最佳问候

解决方案

试试这个



  protected   void  UploadButton_Click( object  sender,EventArgs e)
{
if (FileUploadControl.HasFile)
{
try
{
if (FileUploadControl.PostedFile.ContentType == image / jpeg
{
if (FileUploadControl.PostedFile.ContentLength < 102400
{
string filename = Path.GetFileName(FileUploadControl.Fi leName);
FileUploadControl.SaveAs(Server.MapPath( 〜/)+ filename);
StatusLabel.Text = 上传状态:文件已上传!;
}
else
StatusLabel.Text = 上传状态:文件必须小于100 kb!;
}
else
StatusLabel.Text = 上传状态:只接受JPEG文件!;
}
catch (例外情况)
{
StatusLabel.Text = 上传状态:无法上传文件。出现以下错误: + ex.Message;
}
}
}


当我们将FileControl与UpdatePanel一起使用时,它是一个非常常见的问题。应该使用触发器。



参考



http://stackoverflow.com/questions/9682737/fileupload-hasfile-is-always-false [ ^ ]


在此添加您的更新面板,ASPX页面



 <  触发器 >  
< asp:PostBackTrigger ControlID = btnupload / >
< /触发器 >
< / asp:UpdatePanel > < /跨度>


Hi guys,

I'm working on file upload, but i dont know why its not taking any files.

i have added fileupload in asp.net page with upload button,

i select one file n click upload button, on button its i get the fileone.hasfile as empty.

my code:

protected void btnupload_Click(object sender, EventArgs e)
        {
            if (PhotoUpload1.HasFile)
            {
                Session["ImageBytes"] = PhotoUpload1.FileBytes;
                string str = Server.MapPath(PhotoUpload1.FileName);
                ImagePreview.ImageUrl = "~/DriverSetup/" + PhotoUpload1.FileName;
            }

        }



its not going into if loop, just return false;

plz suggest me,

update panel code:

<pre lang="xml"><asp:UpdatePanel ID="UpdatePanel1" runat="server">
                                      <ContentTemplate>
                                      <asp:Image CssClass="img img-thumbnail" Width="150px" Height="180px" ID="ImagePreview" runat="server" />
                                      <br />
                                      <asp:FileUpload ID="PhotoUpload1" runat="server" />
                                       <br />
                                       <asp:Button ID="btnupload" runat="server" Text="Upload"
                                          onclick="btnupload_Click" />
                                          <asp:UpdateProgress ID="UpdateProgress1" runat="server">
                                          <ProgressTemplate>Please wait...</ProgressTemplate>
                                          </asp:UpdateProgress>
                                      </ContentTemplate>
                                      <Triggers>
                                      <asp:PostBackTrigger ControlID="btnupload" />
                                      </Triggers>
                                      </asp:UpdatePanel>

</pre>



best regards

解决方案

try this

protected void UploadButton_Click(object sender, EventArgs e)
{
    if(FileUploadControl.HasFile)
    {
        try
        {
            if(FileUploadControl.PostedFile.ContentType == "image/jpeg")
            {
                if(FileUploadControl.PostedFile.ContentLength < 102400)
                {
                    string filename = Path.GetFileName(FileUploadControl.FileName);
                    FileUploadControl.SaveAs(Server.MapPath("~/") + filename);
                    StatusLabel.Text = "Upload status: File uploaded!";
                }
                else
                    StatusLabel.Text = "Upload status: The file has to be less than 100 kb!";
            }
            else
                StatusLabel.Text = "Upload status: Only JPEG files are accepted!";
        }
        catch(Exception ex)
        {
            StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
        }
    }
}


Its very common issue when we use FileControl with UpdatePanel. Should use Triggers.

Refer

http://stackoverflow.com/questions/9682737/fileupload-hasfile-is-always-false[^]


Add this in your update panel, ASPX page

<Triggers>
            <asp:PostBackTrigger ControlID="btnupload" />
        </Triggers>
    </asp:UpdatePanel>


这篇关于文件上传无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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