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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:02:59+00:00 2026-05-26T03:02:59+00:00

I recently posted a question asking how I can return a File from a

  • 0

I recently posted a question asking how I can return a File from a byte[] that I am storing.

The question is here byte[] to File Type in MVC 3

Just for reference, I have resulted to storing the binary for the file as a string.

When I return the document, I am calling the following code:

var data = document.BinData.ToByteArray();
return File(data, document.MIMEType,document.Extension);

where BinData is a string, and ToByteArray() is an extension method I wrote:

public static byte[] ToByteArray(this string data)
    {
        byte[] binData= new byte[data.Length];
        for (var i = 0; i < data.Length; i++)
        {
            binData[i] = (byte)data[i];
        }
        return binData;
    }

The problem I am experiencing is, when I get the file back, I am getting something like the following:

enter image description here

Is there anything you can see that I am doing incorrectly?

Edit, I have changed the following “Upload” code to the following:

var stream = BinData.InputStream;
                byte[] data = new byte[BinData.ContentLength];
                stream.Read(data, 0, BinData.ContentLength);

                string documentData = Convert.ToBase64String(data);

where documentData is the string I am storing:

and the download code is the following:

var data = Convert.FromBase64String(document.BinData);
return File(data,document.MimeType,document.Title + document.Extension);

where document.BinData is the documentData from above

  • 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-26T03:02:59+00:00Added an answer on May 26, 2026 at 3:02 am

    I would wager that the problem is in the line:

    Just for reference, I have resulted to storing the binary for the file as a string.

    Based on your decoder (ToByteArray), this is not really encoded in a suitable way; note; you cannot treat arbitrary binary data as a string (at least, not in the way you are trying). The preferred way to do this is simply:

    byte[] originalData = ...
    string asString = Convert.ToBase64String(originalData);
    byte[] reconstructedBinary = Convert.FromBase64String(asString);
    

    (although several other approaches – for example hex – would work too)

    This should give you back the correct byte[], and all should be good from there.


    Re your Stream.Read code, and assuming you already have some kind of oversized length check to prevent from DDOS, note that you should loop on Read:

    int remaining = BinData.ContentLength, offset = 0, bytesRead;
    byte[] data = new byte[remaining];
    while((bytesRead = stream.Read(data, offset, remaining)) > 0) {
        offset += bytesRead;
        remaining -= bytesRead;
    }
    if(remaining > 0) throw new EndOfStreamException();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently posted this question about codes for a gift-card-like voucher that users can
All, I recently posted this question on DAL design. From that it would seem
I recently posted a question asking for what actions would constitute the Zen of
I recently posted a question here about some memory issues I was having. I've
Relatively new to python. I recently posted a question in regards to validating that
Recently i posted a question about what the variable is of a file which
I've recently posted a new question in How jQuery can prevent reopening a new
Recently I posted a answer to a question that I thought was quite an
Recently posted a question regarding the HttpClient over Https ( found here ). I've
I recently posted a question about a WCF Restful web service that I am

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.