0

I have a link like this: C:\folder\folder\image.jpg . How can I save image to database in SQL Server?

If it was a HttpPostedFileBase I would knew how to save it, but now I have only a link.

  • it's a link because I exctracted src tags from tiniMCE editor field .

Thank you.

3 Answers 3

1

Have a look at Download and Upload Images from SQL Server via ASP.Net MVC

Code is also available on codeproject.com

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you . I'll try Pushpendra answer first and then I'll read your link if it doesn't work .
0

Example from msdn social:

table:

CREATE TABLE [dbo].[FileStoreDemo](

                [id] [int] NOT NULL,

                [name] [nvarchar](100) NOT NULL,

                [content] [varbinary](max) NULL,

                CONSTRAINT [pk_filestoredemo_id] PRIMARY KEY CLUSTERED ([id] ASC))

code:

string filename = Server.MapPath("/Content/Desert.jpg")
using (fooEntities fe = new fooEntities())

{

    FileStoreDemo fsd = fe.FileStoreDemoes.CreateObject();



    fsd.name = new FileInfo(filename).Name;

    fsd.content = File.ReadAllBytes(filename);

    fe.FileStoreDemoes.AddObject(fsd);



    fe.SaveChanges();

}

http://social.msdn.microsoft.com/Forums/en/sqlgetstarted/thread/1857938d-b74a-4954-bbde-734dfef08039

Comments

0
   [HttpPost]
        public ActionResult Create(string fileTitle)
        {
            try
            {
                HttpPostedFileBase file = Request.Files[0];
                byte[] imageSize = new byte[file.ContentLength];
                file.InputStream.Read(imageSize, 0, (int)file.ContentLength);
                Image image = new Image()
                {
                    Name = file.FileName.Split('\\').Last(),
                    Size = file.ContentLength,
                    Title = fileTitle,
                    ID = 1,
                    Image1 = imageSize
                };
                db.Images.AddObject(image);
                db.SaveChanges();
                return RedirectToAction("Detail");
            }
            catch(Exception e)
            {
                ModelState.AddModelError("uploadError", e);
            }
            return View();
        }

2 Comments

I got this error : 1. imageshack.us/a/img836/1708/errorsyv.png The code behind is this: 2. imageshack.us/a/img38/4279/code1.png
1. Check model.content and Request.File[0] is not null. The code which I have given you that is work correctly when you are uploading a file from fileuploader and then saving it in to the database but I think you are using the imageSave code as a servie and there is also write down the different code for saving.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.