httpRuntime executionTimeout="6000" maxRequestLength="1097152"
try { String strFile = System.IO.Path.GetFileName(filename); // create an instance fo the web service service.DataService wser = new service.DataService(); FileInfo fInfo = new FileInfo(filename); // to upload it (with the standard 4 MB limit) long numBytes = fInfo.Length; double dLen = Convert.ToDouble(fInfo.Length / 1000000); // Default limit of 4 MB on web server if (dLen < 100) { // set up a file stream and binary reader for the // selected file FileStream fStream = new FileStream(filename, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fStream); // convert the file to a byte array byte[] data = br.ReadBytes((int)numBytes); br.Close(); // pass the byte array (file) and file name to the web service string sTmp = wser.UploadFile(data, strFile); fStream.Close(); fStream.Dispose(); //MessageBox.Show("File Upload Status: " + sTmp, "File Upload"); } else { // Display message if the file was too large to upload MessageBox.Show("The file selected exceeds the size limit for uploads.", "File Size"); } } catch (Exception ex) { // display an error message to the user //MessageBox.Show(ex.Message.ToString(), "Upload Error"); }
[WebMethod] public string UploadFile(byte[] f, string fileName) { try { MemoryStream ms = new MemoryStream(f); FileStream fs = new FileStream(System.Web.Hosting.HostingEnvironment.MapPath ("~/dataupload/") + fileName, FileMode.Create); ms.WriteTo(fs); ms.Close(); fs.Close(); ms = null; fs = null; return "OK"; } catch (Exception ex) { return ex.Message.ToString(); } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)