Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1001129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:37:13+00:00 2026-05-16T07:37:13+00:00

I am trying to upload a file via a form and then save in

  • 0

I am trying to upload a file via a form and then save in in SQL as a blob.

I already have my form working fine, my database is fully able to take the blob and I have a controller that take the file, saves it in a local directory:

[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult FileUpload(int id, HttpPostedFileBase uploadFile)
        {

            //allowed types
            string typesNonFormatted = "text/plain,application/msword,application/pdf,image/jpeg,image/png,image/gif";
            string[] types = typesNonFormatted.Split(',');

            //
            //Starting security check

            //checking file size
            if (uploadFile.ContentLength == 0 && uploadFile.ContentLength > 10000000)
                ViewData["StatusMsg"] = "Could not upload: File too big (max size 10mb) or error while transfering the file.";

            //checking file type
            else if(types.Contains(uploadFile.ContentType) == false)
                ViewData["StatusMsg"] = "Could not upload: Illigal file type!<br/> Allowed types: images, Ms Word documents, PDF, plain text files.";

            //Passed all security checks
            else
            {
                string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads"),
                                                Path.GetFileName(uploadFile.FileName)); //generating path
                uploadFile.SaveAs(filePath); //saving file to final destination

                ViewData["StatusMsg"] = "Uploaded: " + uploadFile.FileName + " (" + Convert.ToDecimal(uploadFile.ContentLength) / 1000 + " kb)";

                //saving file to database
                //
                //MISSING
            }

            return View("FileUpload", null);
        }

Now all I am missing is putting the file in the database. I could not find anything on the subject… I found some way to do it in a regular website but nothing in MVC2.

Any kind of help would be welcome!

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T07:37:14+00:00Added an answer on May 16, 2026 at 7:37 am

    This could help: http://byatool.com/mvc/asp-net-mvc-upload-image-to-database-and-show-image-dynamically-using-a-view/

    Since you have HttpPostedFileBase in your controllers method, all you need to do is:

    int length = uploadFile.ContentLength;
    byte[] tempImage = new byte[length];
    myDBObject.ContentType = uploadFile.ContentType;
    
    uploadFile.InputStream.Read(tempImage, 0, length);
    myDBObject.ActualImage = tempImage ;
    

    HttpPostedFileBase has a InputStream property

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to upload a file via django forms, then send it to an
I'm trying to upload a file via FTP from my local computer to an
I'm trying to upload a file via URLConnection, but I need to read/write it
I'm trying trying to upload a file via Paperclip, but I'm getting an error
I am trying to upload files via FTP using the following script. The file
I'm trying to upload a file .txt in my web space, but then the
I am trying to upload a file with playframework. I create a form and
I am trying to upload images via a form which sits in partial view
I'm trying to upload a file with the Amazon Java SDK, via multipart upload.
I'm working with C# and MVC3 and I'm trying to upload a text file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.