XMl file

Last post 05-08-2008 11:27 PM by Samu Zhang - MSFT. 1 replies.

Sort Posts:

  • XMl file

    05-07-2008, 12:24 PM
    Locked
    adsf
    Rammohan
  • Re: XMl file

    05-08-2008, 11:27 PM

    Hi nanna_rammohan_vsp@hotmail.com, ,

    nanna_rammohan_vsp@hotmail.com:
    I want to upload the xml file and retive the xml file through web services...

     

    So you need to create one Web Service first. Create one web method :

            [WebMethod]
            public string UploadFile(byte[] fs, string FileName)
            {
                try
                {
                    
                    MemoryStream m = new MemoryStream(fs);
                    
                    FileStream f = new FileStream("d:\\" + FileName, FileMode.Create);
                    
                    m.WriteTo(f);
                    m.Close();
                    f.Close();
                    f = null;
                    m = null;
                    return "ok";
                }
                catch (Exception ex)
                {
                    return ex.Message;
                }
            }

     Create one page :

        <form id="form1" runat="server">
        <div>
        
            <asp:FileUpload ID="FileUpload1" runat="server" />
            <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="up" />
        
        </div>
        </form>
      
          protected void Button1_Click(object sender, EventArgs e)
            {
                
    			
    			if(FileUpload1.PostedFile != null)
    			{
    				System.Web.HttpFileCollection oFiles;
    				oFiles = System.Web.HttpContext.Current.Request.Files;
    				if(oFiles.Count < 1)
    				{
    					Response.Write ("please select file");
    					Response.End();
    				}
    
    				string FilePath = oFiles[0].FileName;
    				if(FilePath == "" || FilePath == null)
    				{
                        Response.Write("please select file");
                       
    					Response.End();
    				}
    				string FileName = FilePath.Substring(FilePath.LastIndexOf("\\")+1);
    				try
    				{
    					byte[] b = new byte[oFiles[0].ContentLength];
    					System.IO.Stream fs;
    					localhost.Service1  o;
                        o = new localhost.Service1();
    					fs = (System.IO.Stream)oFiles[0].InputStream;
    					fs.Read(b, 0, oFiles[0].ContentLength);
    					
    					Response.Write(o.UploadFile(b, FileName));
    					fs.Close();
    				}
    				catch(Exception ex)
    				{
    					Response.Write(ex.Message);
    				}
    			}
    			else
    			{
    				Response.Write("please select file");
    			}
    
    		
            }

     

     

    Sincerely,
    Samu Zhang
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Page 1 of 1 (2 items)