コード例 #1
0
ファイル: DbFile.cs プロジェクト: wwwiiilll/CRUDTable
 /// <summary>
 /// Clones this instance.
 /// </summary>
 /// <returns></returns>
 public DbFile Clone()
 {
     DbFile f = new DbFile();
     f.Name = this.Name;
     f.MimeType = this.MimeType;
     f.Data = (byte[])this.Data.Clone();
     return f;
 }
コード例 #2
0
ファイル: CRUDTable.cs プロジェクト: wwwiiilll/CRUDTable
        /// <summary>
        /// Handles the request.
        /// </summary>
        /// <param name="conn">The connection.</param>
        /// <exception cref="System.InvalidOperationException">No table name is available. Try setting `TableName'.</exception>
        protected void HandleRequest(SqlConnection conn)
        {
            if (this.TableName == null) {
                throw new InvalidOperationException("No table name is available. Try setting `TableName'.");
            }

            foreach (string key in this.table.Keys) {
                Field f = this.table.GetField(key);
                f.Value = null;

                if (HttpContext.Current.Request.QueryString[key] != null && f.Type != Field.FieldType.File) {
                    f.Value = HttpContext.Current.Request.QueryString[key];
                }

                if (HttpContext.Current.Request.Form[key] != null && f.Type != Field.FieldType.File) {
                    f.Value = HttpContext.Current.Request.Form[key];
                }

                if (HttpContext.Current.Request.Files[key] != null) {
                    DbFile file = new DbFile();
                    file.Name = HttpContext.Current.Request.Files[key].FileName;
                    file.MimeType = HttpContext.Current.Request.Files[key].ContentType;
                    using (MemoryStream s = new MemoryStream()) {
                        int read = 0;
                        byte[] buffer = new byte[2048];
                        while ((read = HttpContext.Current.Request.Files[key].InputStream.Read(buffer, 0, 2048)) > 0) {
                            s.Write(buffer, 0, read);
                        }
                        file.Data = s.ToArray();
                    }

                    using (MemoryStream s = new MemoryStream()) {
                        BinaryFormatter bf = new BinaryFormatter();
                        bf.Serialize(s, file);
                        f.Value = s.ToArray();
                    }
                }

                if (f.Type == Field.FieldType.File) {
                    if (HttpContext.Current.Request.Form[key + "-remove"] != null && HttpContext.Current.Request.Form[key + "-remove"] == "1") {
                        f.Value = DBNull.Value;
                    }
                }
            }

            switch (HttpContext.Current.Request.QueryString["action"]) {
                case "create":
                    HttpContext.Current.Response.StatusCode = (this.table.Create(conn)) ? 200 : 500;
                    HttpContext.Current.Response.End();
                    break;
                case "update":
                    foreach (string col in this.table.Keys) {
                        if (this.table.GetField(col).Type == Field.FieldType.File && this.table.GetField(col).Value != null) {
                            string cache = "CRUDTable_" + this.TableName + "_file_";
                            foreach (string key in this.table.PrimaryKeys) {
                                cache += this.table.GetField(key).Value + "_";
                            }
                            cache += col;
                            HttpContext.Current.Cache.Remove(cache);
                        }
                    }
                    HttpContext.Current.Response.StatusCode = (this.table.Update(conn)) ? 200 : 500;
                    HttpContext.Current.Response.End();
                    break;
                case "delete":
                    foreach (string col in this.table.Keys) {
                        if (this.table.GetField(col).Type == Field.FieldType.File) {
                            string cache = "CRUDTable_" + this.TableName + "_file_";
                            foreach (string key in this.table.PrimaryKeys) {
                                cache += this.table.GetField(key).Value + "_";
                            }
                            cache += col;
                            HttpContext.Current.Cache.Remove(cache);
                        }
                    }
                    HttpContext.Current.Response.StatusCode = (this.table.Delete(conn)) ? 200 : 500;
                    HttpContext.Current.Response.End();
                    break;
                case "file":
                    if (HttpContext.Current.Request.QueryString["tablecol"] != null) {
                        string cache = "CRUDTable_" + this.TableName + "_file_";
                        foreach (string key in this.table.PrimaryKeys) {
                            cache += this.table.GetField(key).Value + "_";
                        }
                        cache += HttpContext.Current.Request.QueryString["tablecol"];

                        DbFile f = null;
                        if (f == null) {
                            this.table.ReadOne(conn);
                            if (this.table.Count > 0) {
                                try {
                                    f = this.table[HttpContext.Current.Request.QueryString["tablecol"]].GetDbFile(0);
                                    HttpContext.Current.Cache.Insert(cache, f, null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(10));
                                } catch {
                                    HttpContext.Current.Response.StatusCode = 500;
                                }
                            }
                        }
                        if (f != null) {
                            HttpContext.Current.Response.StatusCode = 200;
                            HttpContext.Current.Response.ContentType = f.MimeType;
                            if (f.MimeType.StartsWith("image/") || f.MimeType.StartsWith("text/")) HttpContext.Current.Response.AddHeader("Content-disposition", "inline; filename=" + f.Name);
                            else HttpContext.Current.Response.AddHeader("Content-disposition", "attachment; filename=" + f.Name);
                            HttpContext.Current.Response.BinaryWrite(f.Data);
                        } else {
                            HttpContext.Current.Response.StatusCode = 404;
                        }
                    } else {
                        HttpContext.Current.Response.StatusCode = 404;
                    }
                    HttpContext.Current.Response.End();
                    break;
            }
        }