文件上传控件在ASP.NET中不起作用 [英] fileupload control wouldn't work in ASP.NET

查看:97
本文介绍了文件上传控件在ASP.NET中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ASP.NET创建一个简单的uploadfile控件, 它不会起作用:

I'm trying to make a simple uploadfile control with ASP.NET, and it wouldnt work:

<form id="form1" runat="server">
  <div>
    upload a file now.
      <asp:FileUpload ID="fileupload1" runat="server" />
      <asp:Button  ID="button1"  Text="Upload"  runat="server"  Width="73px" 
            onclick="button1_Click" />
    <asp:Label ID="Label1" runat="server"  Font-Bold="True"  ForeColor="#000099">
         </asp:Label>
  </div>  
</form>

,这是我在(.cs)之后的代码:

if(fileupload1.HasFile)
{
    try
    {
        if(fileupload1.PostedFile.ContentType ==  "image/jpeg")
        {
            if(fileupload1.PostedFile.ContentLength < 51200000)
            {
               string  filename = Path.GetFileName(fileupload1.FileName);
               fileupload1.SaveAs(Server.MapPath("~/img/") + filename);
               Label1.Text ="File uploaded successfully!";
            }
            else
                Label1.Text ="File maximum size is 500 Kb";
        }
        else
            Label1.Text ="Only JPEG files are accepted!";
    }
    catch(Exception exc)
    {
        Label1.Text = "The file could not be uploaded. The following error occured: "
                           + exc.Message;
    }
  }

文件未在服务器中显示. 有什么想法吗?

the file is not presented in the server.. any thoughts?

当我断点时,它们全部生效,应用程序获取代码,它们都可以运行,但是不会将其保存到文件夹中.

when I breakpoint, they all goes valid, the application gets to the code, it all working , but won't save it to the folders.

推荐答案

这可能会完全起作用,也可能不会完全起作用,但是您需要在表单中包含enctype属性.

This may or may not work entirely, but you need to include an enctype attribute in your form.

<form id="form1" runat="server" enctype="multipart/form-data">

如果不这样做,浏览器将不会传输文件.

If you don't do that, browsers won't transfer the file.

请参阅此处: https://developer.mozilla .org/zh-CN/docs/HTML/Element/form#attr-enctype

这篇关于文件上传控件在ASP.NET中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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