2013-11-25 142 views
0

我有ListView,它显示来自XML的信息并从FileUpload保存到服务器上的ListView。我没有问题写入XML,问题是将FileUpload保存到文件夹。列表视图上的文件上传

  1. 我相信,我有错,我没有单独的列表视图来的ItemTemplate和insertemplate和编辑......它必须?因为标题将在XML上更正,甚至文件名。但该文件未保存到文件夹。

  2. 对于我做的大多数测试 - 点击“更新”BTN后没有任何事情发生。当我添加int“我”的项目[我] somtimes它只是更新XML而不保存该文件,有时我会得到“超出索引”的错误。

什么问题?

ASPX代码

<h2><asp:Label ID="LBL_number" runat="server" Text='<%#XPath("id") %>'></asp:Label></h2> 
<h2>Small Image</h2> <asp:Image Width="100" CssClass="ltr" runat="server" ID="TB_small" ImageUrl='<%# XPath("small_image_url") %>'></asp:Image><asp:FileUpload ID="FU_small" runat="server" /> 
<br /><br /><br /> 
<h2>Big Image</h2> <asp:Image Width="300" CssClass="ltr" runat="server" ID="TB_big" ImageUrl='<%#XPath("big_image_url") %>'></asp:Image><asp:FileUpload ID="FU_big" runat="server" /> 
<br /><br /> 
<h2>Title</h2> <asp:TextBox runat="server" ID="TB_title" Text='<%#XPath("title") %>'></asp:TextBox> 
<br /><br /><br /> 
<asp:Button CssClass="btn" ID="Button1" CommandArgument='<%#XPath("id") %>' runat="server" OnClick="update" Text="Update" /> 
<br /> 
<asp:Button ID="Button3" CssClass="btn" CommandArgument='<%#XPath("id") %>' runat="server" CommandName="del" OnClick="del" Text="מחק" /> 
<br /><br /> 
<br /><br /> 
</ItemTemplate> 

</asp:ListView> 
<asp:XmlDataSource ID="XDS_data" runat="server" 
    DataFile="~/App_Data/AM_data.xml" XPath="/Data/datas/data"> 
</asp:XmlDataSource> 

C#实施例仅与小文件上传。

protected void update(object sender, EventArgs e) 
{ 
    XmlDocument doc = new XmlDocument(); 
    doc.Load(Path.Combine(Request.PhysicalApplicationPath, "App_Data/AM_data.xml")); 
    Button myButton = (Button)sender; 
    int i = Convert.ToInt32(myButton.CommandArgument.ToString()); 
    var FU_small1 = (FileUpload)myButton.FindControl("FU_small"); 
    string extenstion_small = System.IO.Path.GetExtension(FU_small1.FileName); 
    filename_small = Guid.NewGuid().ToString(); 

    FileUpload fu2 = LV_data.Items[i].FindControl("FU_small") as FileUpload; 
    if (fu2.HasFile == true) 
    { 
     fu2.SaveAs(Server.MapPath("~/imgs/data/big" + filename_small.ToString() + extenstion_small.ToString()));  
    } 

     var TB_title = (TextBox)myButton.FindControl("TB_title"); 
     string myString3 = TB_title.Text; 

     XmlElement el = (XmlElement)doc.SelectSingleNode("Data/datas/data[id='" + i + "']"); 
     el.SelectSingleNode("small_image_url").InnerText = "~/imgs/data" + filename_small + extenstion_small; 
     el.SelectSingleNode("title").InnerText = myString3; 
     el.SelectSingleNode("big_image_url").InnerText = "~/imgs/data" + filename_big + extenstion_big; 

     doc.Save(Path.Combine(Request.PhysicalApplicationPath, "App_Data/AM_data.xml")); 

     Response.Redirect(Request.RawUrl); 

} 

回答

0

Finnaly “的foreach” 解决它

protected void update(object sender, EventArgs e) 
{ 
    //Get xml file 
    XmlDocument doc = new XmlDocument(); 
    doc.Load(Path.Combine(Request.PhysicalApplicationPath, "App_Data/AM_data.xml")); 
    //Set button for index it later by CommandArgument 
    Button myButton = (Button)sender; 


    foreach (ListViewItem item in LV_data.Items) 
    { 
     //Findcontrol of 2 fileupload on listview 
     FileUpload FU_small1 = (FileUpload)item.FindControl("FU_small1"); 
     FileUpload FU_big1 = (FileUpload)item.FindControl("FU_big1"); 

     //Check if are has file. 
     if (FU_small1.HasFile && FU_big1.HasFile) 
     { 
      //Get extension and genereate random filenames (for avoid ovveride) 
      FileInfo small_info = new FileInfo(FU_small1.FileName); 
      FileInfo big_info = new FileInfo(FU_big1.FileName); 
      string ext_small = small_info.Extension; 
      string ext_big = big_info.Extension; 
      string filename_small = Guid.NewGuid().ToString(); 
      string filename_big = Guid.NewGuid().ToString(); 

      //Set i value by button CommandArgument (look on aspx on question) 
      int i = Convert.ToInt32(myButton.CommandArgument.ToString()); 

      //Get title from TextBox and set string from it 
      TextBox TB_title = myButton.FindControl("TB_title") as TextBox; 
      string myString3 = TB_title != null ? TB_title.Text : string.Empty; 

      //Save the files from the fileuploads we found, and write it on xml 
      FU_small1.SaveAs(Path.Combine(Request.PhysicalApplicationPath, "imgs/data/thumbs/" + filename_small + ext_small)); 
      FU_big1.SaveAs(Path.Combine(Request.PhysicalApplicationPath, "imgs/data/big/" + filename_big + ext_big)); 
      XmlElement el = (XmlElement)doc.SelectSingleNode("Data/datas/data[id='" + i + "']"); 
      el.SelectSingleNode("small_image_url").InnerText = "~/imgs/data/thumbs/" + filename_small + ext_small; 
      el.SelectSingleNode("title").InnerText = myString3; 
      el.SelectSingleNode("big_image_url").InnerText = "~/imgs/data/big/" + filename_big + ext_big; 

      doc.Save(Path.Combine(Request.PhysicalApplicationPath, "App_Data/AM_data.xml")); 
     } 
    } 

    // Refresh the page 
    Response.Redirect(Request.RawUrl); 
    } 
1

有几个问题你update方法:

  1. 你需要找到里面的容器控件,而不是内部的按钮。 * 错误的方法:*var TB_title = (TextBox)myButton.FindControl("TB_title");
  2. 不能保证id将匹配ListView的项目索引。 * 错误的做法:*FileUpload fu2 = LV_data.Items[i].FindControl("FU_small") as FileUpload;

我建议的方法改成这样:

protected void update(object sender, EventArgs e) 
{ 
    int index = 0; 
    XmlDocument doc = new XmlDocument(); 
    doc.Load(Path.Combine(Request.PhysicalApplicationPath, "App_Data/AM_data.xml")); 
    Button myButton = (Button)sender; 
    ListViewItem lvwItem = (ListViewItem)myButton.NamingContainer; 

    FileUpload FU_small1 = myButton.FindControl("FU_small") as FileUpload; 

    if (FU_small1 != null && int.TryParse(myButton.CommandArgument, out index)) 
    { 
     string extenstion_small = System.IO.Path.GetExtension(FU_small1.FileName); 
     filename_small = Guid.NewGuid().ToString(); 

     TextBox TB_title = myButton.FindControl("TB_title") as TextBox; 
     string myString3 = TB_title!= null ? TB_title.Text : string.Empty; 

     if (FU_small1.HasFile == true) 
     { 
      FU_small1.SaveAs(Server.MapPath("~/imgs/data/small/" + filename_small + extenstion_small)); 
     } 
     XmlElement el = (XmlElement)doc.SelectSingleNode("Data/datas/data[id='" + index + "']"); 
     el.SelectSingleNode("small_image_url").InnerText = "~/imgs/data/small/" + filename_small + extenstion_small; 
     el.SelectSingleNode("title").InnerText = myString3; 
     el.SelectSingleNode("big_image_url").InnerText = "~/imgs/data/big/" + filename_big + extenstion_big; 

     doc.Save(Path.Combine(Request.PhysicalApplicationPath, "App_Data/AM_data.xml")); 
    } 

    Response.Redirect(Request.RawUrl); 
} 

而且here的一个测试项目,我已经习惯了进行测试。

+0

@afzaluhlh哇,谢谢你的大力度。我尝试了它,并再次 - 它保存到XML,但它似乎没有得到fileupload文件。奇怪,我相信你测试它,它为你工作...我做了复制粘贴..我会继续尝试。 – Oshrib