.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Sandeep Singh
Melody Anderson
Eminent IT

Home >> Code Snippets >> File Operation >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Save File into Server with or without use webservice in asp.net

Posted By:Sasi Prabhu       Posted Date: November 23, 2012    Points: 40    Category: File Operation    URL: http://www.dotnetspark.com  

Upload files into server without using fileupload control in ASP.Net. This also done with help of webservice also.

Use this Code.

public static string FileUpload(string FilePath,string ToMapPath)
            // get the exact file name from the path
            string FileName = System.IO.Path.GetFileName(FilePath);
            // get the file information form the selected file
            System.IO.FileInfo FileInfo=new System.IO.FileInfo(FileName);
            // get the length of the file to see if it is possible
            // to upload it (with the standard 4 MB limit)
            double Length = Convert.ToDouble(FileInfo.Length/1000000);
            // Default limit of 4 MB on web server
            // have to change the web.config to if
            // you want to allow larger uploads
            if (Length < 4)
                // set up a file stream and binary reader for the
                // selected file
                System.IO.FileStream fStream = new System.IO.FileStream(FileName,
                System.IO.FileMode.Open, System.IO.FileAccess.Read);
                System.IO.BinaryReader br = new System.IO.BinaryReader(fStream);

                // convert the file to a byte array
                byte[] data = br.ReadBytes((int)FileInfo.Length);

                #region Also use in webservice
                // instance a memory stream and pass the
                // byte array to its constructor
                System.IO.MemoryStream ms = new System.IO.MemoryStream(data);
                // instance a filestream pointing to the
                // storage folder, use the original file name
                // to name the resulting file
                System.IO.FileStream fs = new System.IO.FileStream(System.Web.Hosting.HostingEnvironment.MapPath(ToMapPath)+FileName, System.IO.FileMode.Create);
                // write the memory stream containing the original
                // file as a byte array to the filestream
                // clean up



            return "File Uploaded";
The Code inside the region are also done through webservice.


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend