I was hoping someone could shed some light on this. Much to my chagrin, I realized that browsers such as IE8 and IE9 do not support any type of file reader.
So after a bit of research, I'm trying to have the server read the contents of the file, convert it to base64, and then send it back down to the client where the javascript takes it from there.
Is this possible? How would you recommend doing this?
For example right now I have set up a RESTful service that gets the file once the form is submitted...
public string Post()
{
string readableFile="";
HttpResponseMessage result = null;
var httprequest = HttpContext.Current.Request;
if (httprequest.Files.Count > 0)
{
foreach (string file in httprequest.Files)
{
var postedFile = httprequest.Files[file];
//convert to base64? somehow?
}
}
return readableFile;
}
the postedFile variable contains the file information, I would just need to make it into a readable format...though, I am having difficulty.
Any thoughts or help is greatly appreciated!
The HttpPostedFile class has an InputStream property which you can use to read the data. To covert that data to a base64 string you could do the following:
public string Post(HttpPostedFile file)
{
if (file.InputStream.Length > Int32.MaxValue) // Or some other file length limitation
throw new HttpResponseException(HttpStatusCode.BadRequest);
int length = (int)file.InputStream.Length;
byte[] buffer = new byte[length];
file.InputStream.Read(buffer, 0, length);
string encodedString = Convert.ToBase64String(buffer);
return encodedString;
}
You have to code it significantly differently if you want to support larger files over 4GB, but I'm assuming you want to set a reasonable limit on the file size to avoid getting an out of memory exception or having to cache the file to a hard drive.
You're looking for C#, right?
string sixtyfour = System.Convert.ToBase64String(
myFileUploadControl.FileBytes,
0,
myFileUploadControl.FileBytes.Length);
That should take the contents of a fileupload control and put it into base64.
Related
So, i am getting the byte array of a LongRaw image from Oracle...
I am using a webapi to this. After get the array, how i use it on the Client-side ?
Do Its better i convert to base64string and pass this value converting just at the client side ?
cmd.InitialLONGFetchSize = -1;
var reader = cmd.ExecuteReader();
if (reader.Read())
{
// Fetch the LONG RAW
OracleBinary imgBinary = reader.GetOracleBinary(0);
// Get the bytes from the binary obj
byte[] imgBytes = imgBinary.IsNull ? null : imgBinary.Value;
//var imgString = Uri.EscapeDataString(Convert.ToBase64String(imgBytes));
}
//CRIO A LISTA
lretorno.Load(reader, LoadOption.OverwriteChanges, "BUSCAFOTO");
reader.Close();
connection.Close();
connection.Dispose();
var teste = lretorno.Tables[0].AsEnumerable().Select(row => new FotoEnvolvido
{
FOTO = (byte[])(row["FOTO"]),
//FOTO = Convert.ToString(row["FOTO"]),
});
return teste;
You can write a Web API Controller that returns the binary data of an image. Base64 strings impose a overhead of the amount of bytes that have to be transmitted. Please avoid this.
A sample controller can look like this example:
public class WebApiController : ApiController
{
public async Task<HttpResponseMessage> Get(string id)
{
var bytes = await GetBytesFromDataLayerAsync(id);
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
var stream = new MemoryStream(bytes);
result.Content = new StreamContent(stream);
result.Content.Headers.ContentType =
new MediaTypeHeaderValue("image/jpeg");
return result;
}
private async Task<byte[]> GetBytesFromDataLayerAsync(string id)
{
// put your Oracle logic here
return ...
}
}
Depending on what your doing as rboe said writing the bytes directly to the client will save some data size(approx. 37%) and computing overhead. If your not only displaying jpeg images you should also set the mime-type to the correct value... take a look at this source for a rather complete set of extension to mime-type mappings. If you do not know the mime-type you can try "application/octet-stream" as that is the general mime-type for binary data.
If your displaying your content via web browser you could just use an <img> tag something like <img src="view_image.aspx?id=5"> you can even create the dynamically with javascript/jQuery.
If you really do want the image data embedded in a json request which might be useful if you have a lot of little icons and don't want a ton of requests (with http/2 I don't think this will matter) or another reason, then yes first encode the binary data using...
string base64EncodedData = Convert.ToBase64String(bytes);
If the client is javascript you can decode using the latest browsers native functions
var decodedImageData = window.atob(base64EncodedData);
See:
mozilla.org docs
This answer
This answer
If you are however just sending it to another c# endpoint you can use...
byte[] decodedImageData = Convert.FromBase64String(base64EncodedData);
And like I mentioned in the comment to ensure it's encrypted just make the site only support https:// and if you don't have a SSL cert you can get one for free from http://startssl.com
Ack. I am trying to open a specific entry in a zip file archive and store the contents in a string, instead of saving it to a file. I cannot use disk space for this per the client.
Here's what I have:
string scontents = "";
byte[] abbuffer = null;
MemoryStream oms = new MemoryStream();
try
{
//get the file contents
ozipentry.Open().CopyTo(oms);
int length = (int)oms.Length; // get file length
abbuffer = new byte[length]; // create buffer
int icount; // actual number of bytes read
int isum = 0; // total number of bytes read
// read until Read method returns 0 (end of the stream has been reached)
while ((icount = oms.Read(abbuffer, isum, length - isum)) > 0)
{
isum += icount; // sum is a buffer offset for next reading
}
scontents = BytesToString(abbuffer); <----abbuffer is filled with Ascii 0
}
finally
{
oms.Close();
}
The variable abbuffer is supposed to hold that contents of the stream, but all it holds is a bunch of ascii zeros, which I guess means it didn't read (or copy) the stream! But I do not get any error messages or anything. Can someone tell me how to get this working?
I've looked everywhere on stack and on the web, and no where does anyone answer this question specifically for ASP.NET 4.5 ZipArchive library. I cannot use any other library, so if you offer an answer in that, while it would be educational, won't help me at all in this instance. Thanks so much for any help!
One more thing. 'ozipentry' is of type ZipArchiveEntry and is an element in a ZipArchive Entries array. (ie ozipentry = oziparchive.Entries[i])
Oops. One more thing! The function 'BytesToString' is not included, because it is irrelevant. Before the function is called, the abbuffer array is already filled with 0's
Ok. Sorry for being so dense. I realized I was overthinking this. I changed to function to do this:
osr = new StreamReader(ozipentry.Open(), Encoding.Default);
scontents = osr.ReadToEnd();
And it worked fine! Didn't even have to worry about Encoding...
I'm writing a game in C# in Unity 3D (.NET 2.0). To read external files I use File.ReadAllBytes. However, today I encountered a problem one of the files that is clearly not an empty file no corrupted is not read properly - ReadAllBytes returns a byte array with zero elements. The file I'm trying to read is an MP3 file and I could not see anything wrong with it. I can load it in FMODEx, I can play it with a media player, yet ReadAllBytes returns zero. This only happens on that particular file but I'm sure that if it happens with it, then there might be a lot of other files that won't get read.
What can be causing File.ReadAllBytes to malfunction like that and what is the best workaround for this thing ? (eg. if an empty array is returned, read the file in another manner).
Added Code as poster cannot yet:
public void LoadFrom(string filePath)
{
StopTransfer();
byte[] bytes = null;
if (File.Exists(filePath))
{
path = filePath;
name = Path.GetFileNameWithoutExtension(filePath);
extension = Path.GetExtension(filePath);
bytes = File.ReadAllBytes(filePath);
if (bytes.Length == 0) bytes = ReadAllBytes(filePath);
if (bytes.Length == 0) error = "Corrupt file";
fullFile = bytes;
}
else
{
fullFile = null;
error = "Not found";
}
}
I am trying to get the compressed ZIP file back in Javascript. I am able to convert the zip file into Base64 String format. (Zip file is in Server)
Here is my try (at Server Side)
System.IO.FileStream fs = new System.IO.FileStream(SourceFilePath + "Arc.zip", System.IO.FileMode.Open);
Byte[] zipAsBytes = new Byte[fs.Length];
fs.Read(zipAsBytes, 0, zipAsBytes.Length);
String base64String = System.Convert.ToBase64String(zipAsBytes, 0, zipAsBytes.Length);
fs.Close();
if (zipAsBytes.Length > 0)
{
_response.Status = "ZipFile";
_response.Result = base64String;
}
return _json.Serialize(_response);
This part of code returns the JSON data. This JSON data includes the Base64 string. Now what i want to do is to get the original zip file from Base64 string. I searched over the internet but not get the idea.
Is this achievable ?.
It is achievable. First you must convert the Base64 string to an Arraybuffer. Can be done with this function:
function base64ToBuffer(str){
str = window.atob(str); // creates a ASCII string
var buffer = new ArrayBuffer(str.length),
view = new Uint8Array(buffer);
for(var i = 0; i < str.length; i++){
view[i] = str.charCodeAt(i);
}
return buffer;
}
Then, using a library like JSZip, you can convert the ArrayBuffer to a Zip file and read its contents:
var buffer = base64ToBuffer(str);
var zip = new JSZip(buffer);
var fileContent = zip.file("someFileInZip.txt").asText();
JavaScript does not have that functionality.
Theoretically there can be some js library that does this, but it's size probably would be bigger than the original text file itself.
You can also enable gzip compression on your server, so that any output text gets compressed. Most of the browsers would then uncompress the data upon its arrival.
I am forced to use an older version of the SharpZipLib and the standard Microsoft libraries to perform this. I have a gziped file whose name is different than the filename inside the archive. I need to parse the gzip file header to return the original filename. Here is documentation on the gzip website:
http://www.gzip.org/zlib/rfc-gzip.html#conventions
And a java example that looks like it might be doing what I want. it looks like it checks for the file header, but doesn't actually read the file name.
(Sorry couldn't post more than 1 hyperlink)
(http://www).java2s.com/Open-Source/Java-Document/6.0-JDK-Modules/j2me/java/util/zip/GZIPInputStream.java.htm
Any help on this problem would be much appreciated. Thanks!
Well if finally figured it out. Its not the safest way or best but i needed a quick and dirty way to do it and this works. So if anyone else needs to know this or want to improve on it, here you go.
using (FileStream stream = File.OpenRead(filePath))
{
int size = 2048;
byte[] data = new byte[2048];
size = stream.Read(data,0,size);
if (data[3] == 8)
{
List<byte> byteList = new List<byte>();
int i = 10;
while (data[i] != 0)
{
byteList.Add(data[i]);
i++;
}
string test = System.Text.ASCIIEncoding.ASCII.GetString(byteList.ToArray());
Console.WriteLine(test);
}
}