0

We have a client-side control where a user will be able to upload any filetype. The form is serialized into a FormData object and the file is then posted to the server with an ajax call.

var data = new FormData($("#myform")[0]);
$.ajax({
    url: url,
    type: "POST",
    data: data,
    processData: false,
    contentType: false,
    cache: false,
    success: function (d, s, x) { },
    error: function (x, s, e) { },
    complete: function (x) { }
});

In the receiving controller for the posted file, we are creating a new instance of HttpPostedFile based on the HttpRequest.Files collection, and then reading the InputStream into a byte[].

HttpPostedFile file = Request.Files[0];
byte[] fileData = null;
string fileStream;
file.InputStream.Position = 0;
using (var reader = new BinaryReader(file.InputStream, System.Text.Encoding.UTF8))
{
    fileData = reader.ReadBytes(file.ContentLength);
}

After the byte[] has been set, we perform a UTF8.GetString() call on the binary data in order to get the string representation.

fileStream = System.Text.Encoding.UTF8.GetString(fileData);

The issue we are having is that the data of fileStream looks invalid as it contains questionmark replacement characters for some characters. The reason why we are trying to convert the binary data into its string representation, is to store the value of the uploaded file in a database for further use. What are we missing please?

Rudolf Lamprecht
  • 1,050
  • 1
  • 14
  • 37
  • Are you sure it's a UTF8 file then? – DavidG Jun 21 '19 at 13:17
  • I have used the default system encoding as well with no luck – Rudolf Lamprecht Jun 21 '19 at 13:18
  • Have you tried something like this? https://stackoverflow.com/a/14135616/1663001 – DavidG Jun 21 '19 at 13:19
  • I have, same output using streamReader.ReadToEnd() – Rudolf Lamprecht Jun 21 '19 at 13:23
  • What are the contents of the source file being uploaded? – DavidG Jun 21 '19 at 13:24
  • A standard .png image – Rudolf Lamprecht Jun 21 '19 at 13:25
  • So what makes you think you can get text from a png file?! – DavidG Jun 21 '19 at 13:26
  • What we are attempting is to store the InputStream data into a database for further use, are there other means to achieve this then? – Rudolf Lamprecht Jun 21 '19 at 13:30
  • Yes, store the bytes... – DavidG Jun 21 '19 at 13:31
  • The problem is not that you are converting the file into bytes, the problem is that you are converting what you say is a png image into a text file, you can only get question marks... – Martin Verjans Jun 21 '19 at 13:32
  • Or [convert the bytes to a base64 string](https://learn.microsoft.com/en-us/dotnet/api/system.convert.tobase64string) (if you need the string representation, maybe as internal HTML `img src = [embedded base64 encoded Image]`) – Jimi Jun 21 '19 at 16:16
  • @Jimi That is almost certainly a terrible idea, a complete waste of space to store and processing power to do the conversion. – DavidG Jun 21 '19 at 18:01
  • @DavidG It depends on the destionation. If the OP need to just store an image, converting to base64 is then a useless waste. If the Images need to be *embedded* in an HTML file as base64 images, the process of conversion has to be performed at some point. Anyway, it's another method the OP may be interested in (and a little improvement over the text encoding :). – Jimi Jun 21 '19 at 18:25
  • @Jimi Presenting an image as Base64 is pretty rare, while there are occasions it is useful, I'm 99% certain it is not the case here, and I don't want OP to get fixated on converting to a string when it is not necessary. – DavidG Jun 21 '19 at 18:41
  • @DavidG I wouldn't say *pretty rare*, since Google does this all the time (just make an Image search, inspecting the source of the links in the result pages). I don't know what is the OP's purpose, so I can't be sure this is a *fixation* or a choice, but since you're worried I'll say it: *if you just need to store images and nothing else matters, store the raw bytes!*. – Jimi Jun 21 '19 at 19:06
  • Useful comments from you both. The reason why string conversion was attempted, was because of the requirement to encrypt the string for security reasons using an existing method. We resolved the issue by creating a method accepting the byte array for encryption. – Rudolf Lamprecht Jun 24 '19 at 06:36

1 Answers1

-1

I think you have an issue with the serialisation of the byte array. Did you try to use a ajax.beginform() ? this does many work for you...

Ajax.BeginForm in MVC to upload files

Angelo
  • 59
  • 1
  • 2