2013-11-25 55 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