insert plików pdf, doc do bazy sqlServer (blob)

0

Hej,

potrzebuję tego, w necie jest sporo tego ale to zwykły upload. Czy ktoś może podrzucić haczyk lub swoją metodę?
Piszę w MVC, muszę wybrać plik z dysku i zainsertować go do bazy

0

kee? A z czym konkretnie masz problem? Nie umiesz pokazać okna do wybrania pliku, odczytać go z dysku czy zrobić inserta?

0

formularz wiadomo jak ma wyglądać => multi part form data i input type file

  1. Tworzę model np.
 
    public class Plik{
        public string Opis { get; set; }
        public string Nazwa { get; set; }
        public byte[] PlikBinaria { get; set; }
    }
  1. Controller
 
[HttpPost]
public ActionResult Zapisz(Plik plik)
{
    if(Request.Files != null && Request.Files.Count == 1)
    {
        var file = Request.Files[0];
        if (file != null && file.ContentLength > 0)
        {
            var content = new byte[file.ContentLength];
            file.InputStream.Read(content, 0, file.ContentLength);
            plik.PlikBinaria = content;

            // tutaj metoda link to sql patrz pkt 3
        }
    }
    return RedirectToAction("np na listę");    
}
 
        public static void CreateWpis(Plik plik) {
            DB.myContext.InsertOnSubmit(plik);
            DB.SubmitChanges();
        }
0

hehe :) nie

jeszcze raz i co jest nie tak
model

namespace MvcApplication1.Models
{
    public class Plik
    {
        public string Nazwa { get; set; }
        public byte[] PlikBinaria { get; set; }
    }
}
 

controler save

        [HttpPost]
        public ActionResult Save(Plik plik)
        {
            if (Request.Files != null && Request.Files.Count == 1)
            {
                var file = Request.Files[0];
                if (file != null && file.ContentLength > 0)
                {
                    var content = new byte[file.ContentLength];
                    file.InputStream.Read(content, 0, file.ContentLength);
                    plik.PlikBinaria = content;
                    plik.Nazwa = "jakas nazwa";
                    DBProviderDataContext db = new DBProviderDataContext();
                    db.blobs.InsertOnSubmit(plik); // tutaj error
                    db.SubmitChanges();
                    
                    // the rest of your db code here
                }
            }
            return RedirectToAction("Index");  
        }
 

tabelka

 
CREATE TABLE [dbo].[blob](
	[id] [int] NOT NULL,
	[nazwa] [varchar](50) NULL,
	[plik] [varbinary](max) NULL,
 CONSTRAINT [PK_blob] PRIMARY KEY CLUSTERED 
(
	[id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

dwa błędy

Error 2 The best overloaded method match for 'System.Data.Linq.Table<MvcApplication1.Models.blob>.InsertOnSubmit(MvcApplication1.Models.blob)' has some invalid arguments

Error 3 Argument 1: cannot convert from 'MvcApplication1.Models.Plik' to 'MvcApplication1.Models.blob'

0

w ogóle dziwne, że powyższy post został zapisany z nickiem Złoty Krawiec :)

anyway
jeśli zrobię tak:

 
        [HttpPost]
        public ActionResult Save(Plik plik)
        {
            if (Request.Files != null && Request.Files.Count == 1)
            {
                var file = Request.Files[0];
                if (file != null && file.ContentLength > 0)
                {
                    var content = new byte[file.ContentLength];
                    file.InputStream.Read(content, 0, file.ContentLength);
                    
                    var etc = new blob
                    {
                        nazwa = file.FileName,
                        plik = content
                    };

                    DBProviderDataContext db = new DBProviderDataContext();
                    
                    db.blobs.InsertOnSubmit(etc);
                    db.SubmitChanges();
                }
            }
            return RedirectToAction("Index");  
        }

omijam wtedy mój model, a ładniej, chyba, byłoby przepuścić to przez obiekt plik.

1 użytkowników online, w tym zalogowanych: 0, gości: 1