How to use -- Attachments1.Items.Add

Last post 08-15-2009, 4:32 AM by Raime910. 5 replies.
Sort Posts: Previous Next
  •  08-14-2009, 8:23 PM 54704

    How to use -- Attachments1.Items.Add

    Hi! I have a web application that allows users to upload 5 images for their product that they want to sell. I was able restrict them to upload only 5 images without any problem.
     
    Heres an example;
     
    Upload 3 images on postback then insert the product and the images url in my SQLServer table. The problem now is when the user decides to come back and edit the product, what I want to happen is get the 3 images back from the database and populate the Attachments display (the table with paper clip icon and checkbox) and if the user clicks on remove to the existing items it'll remove the files in my web server. Is this possible?
     
    Thanks!
     
    - Ryan
     
  •  08-14-2009, 8:23 PM 54705 in reply to 54704

    RE: How to use -- Attachments1.Items.Add

    Okay I was able to add files into the AttachmentItem not from database but from simple string which I can change later after testing this.
    This is what I have;
     
    1. protected override void OnInit(EventArgs e)  
    2. {  
    3.     base.OnInit(e);  
    4.     SampleUtil.SetPageCache();  
    5.       
    6.     string str = Server.MapPath("uploaded/img1.jpg");  
    7.     System.Net.WebRequest req = System.Net.WebRequest.Create(str);  
    8.     Stream streamImg = req.GetResponse().GetResponseStream();  
    9.   
    10.     Response.Write(streamImg.ToString());  
    11.   
    12.     Attachments1.Items.Add(192, "img1Filename", streamImg);  
    13.     Attachments1.AttachmentAdded += new AttachmentItemEventHandler(Attachments1_AttachmentAdded);  
    14.     ButtonDeleteAll.Click += new EventHandler(ButtonDeleteAll_Click);  
    15.     ButtonTellme.Click += new EventHandler(ButtonTellme_Click);  

     Now when the page loads I have 1 item which I added using the code above, the problem is whenever I try to delete it I get an error that says;
     
    Error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.

    Details: Error parsing near 'System.Net.FileWebSt'.
     
    Any idea what this is? I think Im using the Attachment1.Items.Add and the Stream wrong.
    - Ryan
  •  08-15-2009, 1:53 AM 54709 in reply to 54705

    Re: RE: How to use -- Attachments1.Items.Add

    Ryan
     
    Please try System.IO.FileStream , instead of the System.Net.WebRequest.Create(str); 
     
    You need dispose the streamImg after the .Add method too.
     
    Regards,
    Terry
  •  08-15-2009, 2:30 AM 54710 in reply to 54709

    Re: RE: How to use -- Attachments1.Items.Add

    Sorry but how do I do that? Can you give me an example? Thanks!
  •  08-15-2009, 3:54 AM 54711 in reply to 54710

    Re: RE: How to use -- Attachments1.Items.Add

    Hi,
     
    Please check this sample code :
     
    1. protected override void OnLoad(EventArgs e)   
    2. {   
    3.     base.OnLoad(e);   
    4.   
    5.     //need check is postback or not too!   
    6.     if (!Page.IsPostBack)   
    7.     {   
    8.         string str = Server.MapPath("uploaded/img1.jpg");   
    9.         using (System.IO.FileStream streamImg = new System.IO.FileStream(str, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read))   
    10.         {   
    11.   
    12.             Response.Write(streamImg.ToString());   
    13.                
    14.             Attachments1.Items.Add(192, "img1Filename", streamImg);   
    15.         }   
    16.     }   
    17.     Attachments1.AttachmentAdded += new AttachmentItemEventHandler(Attachments1_AttachmentAdded);     
    18.     ButtonDeleteAll.Click += new EventHandler(ButtonDeleteAll_Click);     
    19.     ButtonTellme.Click += new EventHandler(ButtonTellme_Click);     
    20. }  
     
    Regards,
    Terry
  •  08-15-2009, 4:32 AM 54712 in reply to 54711

    Re: RE: How to use -- Attachments1.Items.Add

    That worked pretty darn well!

    Thanks!
     
    - Ryan
View as RSS news feed in XML