将文件保存在SQL Server数据库中

时间:2015-08-25 14:25:58

标签: c# sql-server file-upload linq-to-sql blobstore

我有一个处理程序,它从Javascript代码获取blolbs并将它们保存在一个文件夹中,这个解决方案是上传大文件。但现在我想将数据保存到数据库中 我创建了一个Linq-to-SQL模型,并创建了一个包含字段id,name,data,filetype的数据库 任何人都可以帮我保存db中的数据吗?

这是我的代码:

namespace test
{
    /// <summary>
    /// Summary description for Handler
    /// </summary>
    public class Handler : IHttpHandler
    {
        LinfSqlDataContext LINQSql = new LinfSqlDataContext();
        testfilesave ft = new testfilesave();
        public static void AppendAllBytes(string path, byte[] bytes)
        {

            //argument-checking here.
            try
            {

                using (var stream = new FileStream(path, FileMode.Append))
                {
                    stream.Write(bytes, 0, bytes.Length);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        public void ProcessRequest(HttpContext context)
        {
            try
            {

                byte[] buffer = new byte[context.Request.ContentLength];
                context.Request.InputStream.Read(buffer, 0, context.Request.ContentLength);
                string fileName = context.Request.Headers.Get("X_FILE_NAME");

                string filePath = context.Server.MapPath("~/upload/" + fileName);
                if (context.Request.Headers.Get("X_Blob_Number") == "0")
                {
                    if (File.Exists(filePath))
                    {
                        File.Delete(filePath);
                    }
                }
                AppendAllBytes(context.Server.MapPath("~/upload/" + fileName), buffer);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

0 个答案:

没有答案
相关问题