none
FileUpload控件问题 RRS feed

  • 问题

  • 我用FileUpload上传文件,可是HasFile属性总是False,不管我有没有选择文件。

    后来我用ajax tool kit的AsyncFileUpload,然后选择完文件就提示File Attached Invalid。

    我的代码:

    <%@ Page Title="管理乐谱" Language="C#" MasterPageFile="~/Content/Default.Master" AutoEventWireup="true" CodeBehind="Manage.aspx.cs" Inherits="MusicGod.Scores.Manage" %>
    
    <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="Head" runat="server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="Main" runat="server">
        <asp:ListView ID="ListView1" DataKeyNames="Id" runat="server" InsertItemPosition="LastItem" ItemType="MusicGod.Models.MusicPage" SelectMethod="ListView1_GetData" InsertMethod="ListView1_InsertItem" DeleteMethod="ListView1_DeleteItem">
            <InsertItemTemplate>
                <li>
                    页码: 
                    <asp:TextBox ID="Num" runat="server" Text='<%# BindItem.Num %>'/>
                    <asp:RequiredFieldValidator ID="reqNum" ErrorMessage="请输入页码" ControlToValidate="Num" runat="server" Text="*" CssClass="ErrorMsg" /><br />
    
                    图像文件: 
                    <ajax:AsyncFileUpload ID="UrlUpload" runat="server" />
                    <asp:CustomValidator ID="cusUrl" ErrorMessage="请选择 .png 文件" runat="server" Text="*" CssClass="ErrorMsg" /><br />
    
                    <asp:Button ID="InsertBtn" Text="添加" runat="server" CommandName="Insert" /><br />
                    <asp:ValidationSummary ID="ValidationSummary1" runat="server" />
                </li>
            </InsertItemTemplate>
            <ItemTemplate>
                <li>
                    <asp:Image ImageUrl='<%# Item.Url %>' runat="server" /><br />
                    第<asp:Literal ID="Num" runat="server" Text='<%# Item.Num %>'></asp:Literal>页<br />
    
                    <asp:Button ID="DeleteBtn" Text="删除" runat="server" CommandName="Delete" />
                </li>
            </ItemTemplate>
            <LayoutTemplate>
                <h1>管理乐谱</h1>
                <ul class="ItemContainer">
                    <li id="itemPlaceHolder" runat="server"></li>
                </ul>
            </LayoutTemplate>
        </asp:ListView>
    </asp:Content>
    

    public void ListView1_InsertItem([QueryString("ScoreId")]int scoreId)
            {
                var item = new MusicGod.Models.MusicPage();
                TryUpdateModel(item);
                AsyncFileUpload urlUpLoad = (AsyncFileUpload)ListView1.InsertItem.FindControl("UrlUpload");
                if (!urlUpLoad.HasFile || !urlUpLoad.FileName.ToLower().EndsWith(".png"))
                {
                    CustomValidator cusUrl = (CustomValidator)ListView1.InsertItem.FindControl("cusUrl");
                    cusUrl.IsValid = false;
                    ModelState.AddModelError("Invalid", cusUrl.ErrorMessage);
                }
                if (ModelState.IsValid && Page.IsValid)
                {
                    // Save changes here
                    using (var entities = new MGEntities())
                    {
                        item.ScoreId = scoreId;
    
                        string virFolder = "~/Resrc/";
                        string serverPath = Server.MapPath(virFolder);
                        string fileName = Guid.NewGuid().ToString();
                        string ext = ".png";
    
                        urlUpLoad.SaveAs(Path.Combine(serverPath, fileName + ext));
    
                        item.Url = virFolder + fileName + ext;
    
                        entities.MusicPages.Add(item);
                        entities.SaveChanges();
                        
                    }
                }
            }

    2015年7月17日 8:04

答案