Array of bytes to file

I have a byte array for a file, is there a way to take it and save it to a file with a remote file server?

Thanks, core.

+4
source share
3 answers
File.WriteAllBytes(@"\\server\public_share\MyFile.txt", byteArray); 
+10
source

Writing your data to a file is the easy part, and @aaron showed you how ...

 ie File.WriteAllBytes(....etc 

But what you need to know if you transmit binary data over a wire, and if your data contains bytes that can be interpreted as control characters, then your data transfer will be problematic.

What you may need is to first encode your data so that you can transfer it safely, as a rule, you would use something like Base64 encoding.

You can use the Convert helper class to do this ...

 Convert.ToBase64String("file contents"); 
0
source

If you do this in code, then you will need to use FileStream and BinaryWriter objects.

Something like that;

 FileStream filestream = new FileStream("myfile.txt", FileMode.Open); BinaryReader br = new BinaryReader(filestream); String msg = br.ReadString(); br.Close(); filestream.Close(); FileStream networkStream = new FileStream(@"\\server\share\file.txt", FileMode.Create); BinaryWriter bw = new BinaryWriter(filestream); bw.Write(msg); bw.Close(); networkStream.Close(); 

If you pass it through Javascript, perhaps using the HTML view button, then you will need to do the same, but you will get a stream of files from the message form request.

You may have trouble writing to a network location, if you are using IIS, then you can configure the virtual directory and set the credentials in IIS. An alternative is that you will need to do an impersonation in order to write the file to a network server.

Mike

0
source

Source: https://habr.com/ru/post/1335151/


All Articles