Read Byte Array from a zip file in project folder in WP7 - c#

I have a zip file and I have to read its bytes and decrypt. How do I get the byte array from the file which I have added to the project and set its bulid action property as content. Help me.

You can get the bytes of the file this way:
var res = Application.GetResourceStream(new Uri("yourFile", UriKind.Relative));
var fileStream = res.Stream;
byte[] buffer = new byte[fileStream.Length];
fileStream.Read(buffer, 0, (int)fileStream.Length);

Use This.
return File.ReadAllBytes( shapeFileZip.Name );
Its use For C# code.

Related

How to get the ZIP file from Base64 string in Javascript?

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.

PDF Upload error System.IO.DirectoryNotFoundException: Could not find a part of the path

I have developed an ASP.net C# function to upload PDF to the Database. when I try it in LocalHost, its working perfectly fine. but when I publish it in a server on IIS. it gives me the below error when I click upload:
System.IO.DirectoryNotFoundException: Could not find a part of the
path + <path of the file>
string filePath = Path.GetFullPath(FileUpload1.PostedFile.FileName);
string filename = Path.GetFileName(filePath);
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
br.Close();
fs.Close();
Is there anything I should change in order to be able to upload?
When you access it from localhost, both client and server are same so it can find file. but when you publish both the machine are isolated. basically you are not getting content from uploaded file, what you did is get the filename and fetch data from local hard disk, you should use following snippet.
int fileLen = fu.PostedFile.ContentLength;
Byte[] Input = new Byte[fileLen];
Stream myStream = fu.PostedFile.InputStream;
myStream.Read(Input, 0, Input.Length);
I have declared an byte array with size of bytes in uploaded file. and read byte from PostedFile InputStream.

Store Image in .resx as byte[] rather than Bitmap

Slightly daft, but...
Is there a way to prevent Visual Studio treating a .jpg file in a .resx as a Bitmap so that I can access a byte[] property for the resource instead?
I just want:
byte[] myImage = My.Resources.MyImage;
Alternatively, right click on your .resx file and click "View Code".
Edit the XML resource item to use System.Byte[] like this:
<data name="nomap" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\nomap.png;System.Byte[]</value>
</data>
Save and you should be able to use Byte[] instead of Bitmap
Try using an "Embedded Resource" instead
So lets say you have a jpg "Foo.jpg" in ClassLibrary1. Set the "Build Action" to "Embedded Resource".
Then use this code to get the bytes
byte[] GetBytes()
{
var assembly = GetType().Assembly;
using (var stream = assembly.GetManifestResourceStream("ClassLibrary1.Foo.jpg"))
{
var buffer = new byte[stream.Length];
stream.Read(buffer, 0, (int) stream.Length);
return buffer;
}
}
Or, alternatively, if you want a more re-usable method
byte[] GetBytes(string resourceName)
{
var assembly = GetType().Assembly;
var fullResourceName = string.Concat(assembly.GetName().Name, ".", resourceName);
using (var stream = assembly.GetManifestResourceStream(fullResourceName))
{
var buffer = new byte[stream.Length];
stream.Read(buffer, 0, (int) stream.Length);
return buffer;
}
}
and call
var bytes = GetBytes("Foo.jpg");
Give the jpeg file a different extension, such as "myfile.jpeg.bin". Visual studio should then treat it as binary file and the generated designer code will return byte[].

Using plupload control with .NET

I downloaded the source project from the website, using as is, except I changed the target file from upload.php to upload.aspx, which contains the following code to receive the file data:
int chunk = Request.QueryString["chunk"] != null ? int.Parse(Request.QueryString["chunk"]) : 0;
string fileName = Path.GetFileName(Request.Files[0].FileName);
// Read stream
BinaryReader br = new BinaryReader(Request.InputStream);
byte[] buffer = br.ReadBytes((int)br.BaseStream.Length);
br.Close();
//byte[] appended = buffer.Take(149).ToArray();
// Write stream
BinaryWriter bw = new BinaryWriter(File.Open(Server.MapPath("~/uploadfiles" + fileName), chunk == 0 ? FileMode.Create : FileMode.Append));
bw.Write(buffer);
bw.Close();
The problem is when I upload a jpg file, or any other file, there is data prepended and appended to every chunk, that obviously makes the file corrupted, and increases the file size. Any idea why that would happen?
You ned to read from Request.Files[0] not from Request.InputStream.
see marco's post: here

convert base64Binary to pdf

I have raw data of base64Binary.
string base64BinaryStr = "J9JbWFnZ......"
How can I make pdf file? I know it need some conversion. Please help me.
Step 1 is converting from your base64 string to a byte array:
byte[] bytes = Convert.FromBase64String(base64BinaryStr);
Step 2 is saving the byte array to disk:
System.IO.FileStream stream =
new FileStream(#"C:\file.pdf", FileMode.CreateNew);
System.IO.BinaryWriter writer =
new BinaryWriter(stream);
writer.Write(bytes, 0, bytes.Length);
writer.Close();
using (System.IO.FileStream stream = System.IO.File.Create("c:\\temp\\file.pdf"))
{
System.Byte[] byteArray = System.Convert.FromBase64String(base64BinaryStr);
stream.Write(byteArray, 0, byteArray.Length);
}
First convert the Bas64 string to byte[] and write it into a file.
byte[] bytes = Convert.FromBase64String(base64BinaryStr);
File.WriteAllBytes(#"FolderPath\pdfFileName.pdf", bytes );
This code does not write any file on the hard drive.
Response.AddHeader("Content-Type", "application/pdf");
Response.AddHeader("Content-Length", base64Result.Length.ToString());
Response.AddHeader("Content-Disposition", "inline;");
Response.AddHeader("Cache-Control", "private, max-age=0, must-revalidate");
Response.AddHeader("Pragma", "public");
Response.BinaryWrite(Convert.FromBase64String(base64Result));
Note: the variable base64Result contains the Base64-String: "JVBERi0xLjMgCiXi48/TIAoxI..."
All you need to do is run it through any Base64 decoder which will take your data as a string and pass back an array of bytes. Then, simply write that file out with pdf in the file name.
Or, if you are streaming this back to a browser, simple write the bytes to the output stream, marking the appropriate mime-type in the headers.
Most languages either have built in methods for converted to/from Base64. Or a simple Google with your specific language will return numerous implementations you can use. The process of going back and forth to Base64 is pretty straightforward and can be implemented by even novice developers.
base64BinaryStr - from webservice SOAP message
byte[] bytes = Convert.FromBase64String(base64BinaryStr);

Categories