protected void ButtonUpload_Click(object sender, EventArgs e) { if (Page.IsValid) { if (FileUpload.HasFile) { // a file has been uploaded, check if it has content, // and if the content size does not exceed the maximum // as set by the administrator in the settings HttpPostedFile httpFile = FileUpload.PostedFile; if (httpFile.ContentLength > 0 && httpFile.ContentLength < BlogoSettings.MaxFileSize) { // the file size is valid, let's put it into the database Blogo.NET.Business.File newFile = new Blogo.NET.Business.File(); newFile.filename = System.IO.Path.GetFileName(httpFile.FileName); newFile.mime = httpFile.ContentType; newFile.filecontent = new byte[httpFile.ContentLength]; httpFile.InputStream.Read(newFile.filecontent, 0, httpFile.ContentLength); FileManager.Save(newFile); Response.Redirect("~/View/Pages/Admin/AdminFiles.aspx"); } else { // a file was uploaded but the file's content size was invalid LabelError.Text = "The file size is invalid (either 0 or exceeding the maximum)"; // SPECIAL NOTE: When the uploaded file is larger than the setting // maxRequestLength in the web.config (httpRuntime setting), // users will not face our friendly error message. Instead, // they will get a brute "connect was reset" error from the browser. // to avoid this, increase the maxRequestLength setting } } } }
protected void Page_Load(object sender, EventArgs e) { // check if a file identifier was passed... if (Request.QueryString["file"] != null) { // get the file object long fileID = long.Parse(Request.QueryString["file"].ToString()); Blogo.NET.Business.File myFile = FileManager.GetItem(fileID); if (myFile != null) { // a file object was found in the database, output the file contents Response.Clear(); Response.ContentType = myFile.mime; Response.OutputStream.Write((byte[])myFile.filecontent, 0, (int)myFile.filecontent.Length); Response.End(); } else { // now file was found matching the file identifier throw new Exception("Invalid file identifier: " + fileID.ToString()); } } }