Ejemplo n.º 1
0
        public ActionResult MultiFileUploadDB(IEnumerable<HttpPostedFileBase> files )
        {
            string message = null;
            foreach (var file in files)
            {
                if (file != null && file.ContentLength > 0)
                {
                    string fileName = Path.GetFileName(file.FileName);
                    int length = file.ContentLength;
                    byte[] buffer = new byte[length];
                    // 讀取Stream,寫入buffer
                    file.InputStream.Read(buffer, 0, length);

                    DbFile dbfile = new DbFile()
                    {
                        Name = fileName,
                        MimeType = file.ContentType,
                        Size = file.ContentLength,
                        Content = buffer
                    };
                    try
                    {
                        db.DbFiles.Add(dbfile);
                        db.SaveChanges();
                        message += fileName + "上傳成功.<br>";
                        TempData["Message"] += message;
                    }
                    catch (Exception ex)
                    {
                        TempData["Message"] = "儲存錯誤:" + ex.Message;
                    }
                }
                else
                {
                    TempData["Message"] = "未選擇或空白檔案。";
                }
            }
            
            return View();
        }
Ejemplo n.º 2
0
        public ActionResult UploadToDB(HttpPostedFileBase file)
        {
            if (file != null && file.ContentLength > 0)
            {
                string fileName = Path.GetFileName(file.FileName);
                int length = file.ContentLength;
                byte[] buffer = new byte[length];
                // 讀取Stream,寫入buffer
                file.InputStream.Read(buffer, 0, length);

                DbFile dbfile = new DbFile()
                {
                    Name = fileName,
                    MimeType = file.ContentType,
                    Size = file.ContentLength,
                    Content = buffer
                };
                try
                {
                    db.DbFiles.Add(dbfile);
                    db.SaveChanges();
                    string message = "Name:" + fileName + ",<br>" +
                                     "Content Type:" + file.ContentType + ",<br>" +
                                     "Size:" + file.ContentLength + ",<br>" +
                                     "上傳成功.";
                    TempData["Message"] = message;
                }
                catch (Exception ex)
                {
                    TempData["Message"] = "儲存錯誤:" + ex.Message;
                }
            }
            else
            {
                TempData["Message"] = "未選擇或空白檔案。";
            }
            return View();
        }