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 8224019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:54:42+00:00 2026-06-07T14:54:42+00:00

I have a file input control. <input type=file name=file id=SaveFileToDB/> Lets say I browse

  • 0

I have a file input control.

   <input type="file" name="file" id="SaveFileToDB"/>

Lets say I browse to C:/Instruction.pdf document and click on submit. On Submit, I want to save the document in RavenDB and also later retrieve it for download purposes. I saw this link http://ravendb.net/docs/client-api/attachments
that says.. do this..

Stream data = new MemoryStream(new byte[] { 1, 2, 3 }); 

documentStore.DatabaseCommands.PutAttachment("videos/2", null, data,
  new RavenJObject {{"Description", "Kids play in the garden"}});

I am not following what 1,2,3 mean here and what it means to say videos/2 in the command… how I can use these two lines to use it in my case.. to save word/pdfs in ravendb.. if any one has done such thing before, please advise.

I am not clear on one thing.. how the attachment is stored. If I want to store the attachment itself (say pdf) it is stored independently in ravendb.. and I just store the key of the attachment in the main document that it is associated with? If that is so, where is the pdf stored physically in ravendb? can I see it?

  • 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-06-07T14:54:43+00:00Added an answer on June 7, 2026 at 2:54 pm

    The 1,2,3 is just example data. What it is trying to get across is that you create a memory stream of whatever you want then use that memory stream in the PutAttachment method. Below is ad-hoc and not tested but should work:

            using (var mem = new MemoryStream(file.InputStream)
            {
                _documentStore.DatabaseCommands.PutAttachment("upload/" + YourUID, null, mem,
                                                              new RavenJObject
                                                                  {
                                                                      { "OtherData", "Can Go here" }, 
                                                                      { "MoreData", "Here" }
                                                                  });
            }
    

    Edited for the rest of the questions

    1. How is attachment stored? I believe it is a json document with one property holding the byte array of the attachment
    2. Is the “document” stored independently? Yes. An attachment is a special document that is not indexed but it is part of the database so that tasks like replication work.
    3. “Should I” store the key of the attachment in the main document that it is associated with? Yes you would reference the Key and anytime you want to get that you would just ask Raven for the attachment with that id.
    4. Is the pdf stored physically in ravendb? Yes.
    5. Can you see it? No. It does even show up in the studio (at least as far as I know)

    Edit Corrected and Updated Sample

            [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Upload(HttpPostedFileBase file)
        {
            byte[] bytes = ReadToEnd(file.InputStream);
            var id = "upload/" + DateTime.Now.Second.ToString(CultureInfo.InvariantCulture);
            using (var mem = new MemoryStream(bytes))
            {
                DocumentStore.DatabaseCommands.PutAttachment(id, null, mem,
                                                              new RavenJObject
                                                              {
                                                                  {"OtherData", "Can Go here"},
                                                                  {"MoreData", "Here"},
                                                                  {"ContentType", file.ContentType}
                                                              });
            }
    
            return Content(id);
        }
    
        public FileContentResult GetFile(string id)
        {
            var attachment = DocumentStore.DatabaseCommands.GetAttachment("upload/" + id);
            return new FileContentResult(ReadFully(attachment.Data()), attachment.Metadata["ContentType"].ToString());
        }
    
        public static byte[] ReadToEnd(Stream stream)
        {
            long originalPosition = 0;
    
            if (stream.CanSeek)
            {
                originalPosition = stream.Position;
                stream.Position = 0;
            }
    
            try
            {
                var readBuffer = new byte[4096];
    
                int totalBytesRead = 0;
                int bytesRead;
    
                while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0)
                {
                    totalBytesRead += bytesRead;
    
                    if (totalBytesRead == readBuffer.Length)
                    {
                        int nextByte = stream.ReadByte();
                        if (nextByte != -1)
                        {
                            var temp = new byte[readBuffer.Length*2];
                            Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length);
                            Buffer.SetByte(temp, totalBytesRead, (byte) nextByte);
                            readBuffer = temp;
                            totalBytesRead++;
                        }
                    }
                }
    
                byte[] buffer = readBuffer;
                if (readBuffer.Length != totalBytesRead)
                {
                    buffer = new byte[totalBytesRead];
                    Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead);
                }
                return buffer;
            }
            finally
            {
                if (stream.CanSeek)
                {
                    stream.Position = originalPosition;
                }
            }
        }
    
        public static byte[] ReadFully(Stream input)
        {
            byte[] buffer = new byte[16 * 1024];
            using (MemoryStream ms = new MemoryStream())
            {
                int read;
                while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }
                return ms.ToArray();
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a form that has a file input control: <input type=file onclick=this.blur() name=descFile
I have a file upload control <input id=File1 type=file /> in my page... How
Using browser firefox and chrome I have an input file element. <input type='file' id='tempFileInput'
I have a page with File html input field and a Submit button as
the html on the page contains: <input type=File name=File size=70 value= class=inputfield_en> I'm trying
I have a class which enables forms with a file-type input to be submitted
I have a asp.net form with 5 HTML file input controls with runat=server and
I have a list of file input boxes and I want to know if
If I have an input file like: US, P1, AgriZone, H, 1000, 1200, 1101,
I have created a JAR file in this way jar cf jar-file input-files .

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.