DataLIst中的FileUpload

时间:2014-04-16 09:00:17

标签: c# asp.net file-upload datalist

我有数据专家。每个项目都包含FileUpload控件。 如何在每次上传后检查上传的文件?

因为我不知道,我试图做其他事情 - 按钮点击后会进行检查。

注意:此datalist是其他datalist的子项。这是嵌套的datalist。父级的数据源是订单的arraylist,子级的数据源是每个订单中的产品列表。

 private void UploadProduct(DataListItem dli, ArrayList pl) // pl = productsList
{
    int i = 0;
    DataList DataList2 = (DataList)dli.FindControl("DataList2");
    foreach (DataListItem item in DataList2.Items)
    {
        Product p = pl[i] as Product;
        FileUpload FileUpload1 = (FileUpload)item.FindControl("FileUpload1");
        if (FileUpload1.PostedFile.FileName != "" && FileUpload1.PostedFile.ContentType.Contains("jpg") && FileUpload1.PostedFile.ContentLength <= 2097151)
        {
            p.UploadedProduct = FileUpload1.FileBytes; // pc.UploadedProduct is byte[]
        }

        i++;
    }
}    

在Item_Command中:

    if (e.CommandName == "finishOrder")
    {
        ArrayList ordersList = (ArrayList)Session["OrdersList"]; // the datasource of the outer datalist

        int orderID = Convert.ToInt32(e.CommandArgument);
        ProductsCart pc = null;
        foreach (ProductsCart productsCart in ordersList) // to get the object which the button refers to
        {
            if (productsCart.OrderID == orderID)
                pc = productsCart;

        }

        DataListItem item = ((DataListItem)((Button)e.CommandSource).NamingContainer); // to get the datalist item of the clicked button
        FileUpload FileUpload1 = (FileUpload)(item.FindControl("FileUpload1"));
      //  Label errorMsg = (Label)(item.FindControl("errorMsg"));



        bool flag = true;
       // string imagePath;

        UploadProduct(item, pc.ProductsList);

        foreach (Product p in pc.ProductsList) // the check if the uploads were uploaded according to my conditions
        {
            if (p.UploadedProduct==null)
            {
                flag = false;
            }
        }

        if (flag)
        {
            // do something
        }
        else // error
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('error');", true);
        }

        FillDataListOuter(DataList1); // datasource and databind   
    }

这不起作用,这就是我寻求帮助的原因。当我尝试上传docx文件时,如果flag为true,它会尝试执行代码。所以我想这不是一个好方法。 我需要帮助,因为我试着阅读了很多,但我找不到答案。

0 个答案:

没有答案