I'm trying to write out a Byte[]
array representing a complete file to a file.
The original file from the client is sent via TCP and then received by a server. The received stream is read to a byte array and then sent to be processed by this class.
This is mainly to ensure that the receiving TCPClient
is ready for the next stream and separate the receiving end from the processing end.
The FileStream
class does not take a byte array as an argument or another Stream object ( which does allow you to write bytes to it).
I'm aiming to get the processing done by a different thread from the original ( the one with the TCPClient).
I don't know how to implement this, what should I try?
Based on the first sentence of the question: "I'm trying to write out a Byte[] array representing a complete file to a file."
The path of least resistance would be:
File.WriteAllBytes(string path, byte[] bytes)
Documented here:
You can use a BinaryWriter
object.
protected bool SaveData(string FileName, byte[] Data)
{
BinaryWriter Writer = null;
string Name = @"C:\temp\yourfile.name";
try
{
// Create a new stream to write to the file
Writer = new BinaryWriter(File.OpenWrite(Name));
// Writer raw data
Writer.Write(Data);
Writer.Flush();
Writer.Close();
}
catch
{
//...
return false;
}
return true;
}
Edit: Oops, forgot the finally
part... lets say it is left as an exercise for the reader ;-)
有一个静态方法System.IO.File.WriteAllBytes
You can do this using System.IO.BinaryWriter
which takes a Stream so:
var bw = new BinaryWriter(File.Open("path",FileMode.OpenOrCreate);
bw.Write(byteArray);
You can use the FileStream.Write(byte[] array, int offset, int count) method to write it out.
If your array name is "myArray" the code would be.
myStream.Write(myArray, 0, myArray.count);
是的,为什么不呢?
fs.Write(myByteArray, 0, myByteArray.Length);
Try BinaryReader:
/// <summary>
/// Convert the Binary AnyFile to Byte[] format
/// </summary>
/// <param name="image"></param>
/// <returns></returns>
public static byte[] ConvertANYFileToBytes(HttpPostedFileBase image)
{
byte[] imageBytes = null;
BinaryReader reader = new BinaryReader(image.InputStream);
imageBytes = reader.ReadBytes((int)image.ContentLength);
return imageBytes;
}
// This is server path, where application is hosted.
var path = @"C:\Websites\mywebsite\profiles\";
//file in bytes array
var imageBytes = client.DownloadData(imagePath);
//file extension
var fileExtension = System.IO.Path.GetExtension(imagePath);
//writing(saving) the files on given path. Appending employee id as file name and file extension.
File.WriteAllBytes(path + dataTable.Rows[0]["empid"].ToString() + fileExtension, imageBytes);
You may need to Provide access to profile folder for iis user.
public ActionResult Document(int id)
{
var obj = new CEATLMSEntities().LeaveDocuments.Where(c => c.Id == id).FirstOrDefault();
string[] stringParts = obj.FName.Split(new char[] { '.' });
string strType = stringParts[1];
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("content-disposition", "attachment; filename=" + obj.FName);
var asciiCode = System.Text.Encoding.ASCII.GetString(obj.Document);
var datas = Convert.FromBase64String(asciiCode.Substring(asciiCode.IndexOf(',') + 1));
//Set the content type as file extension type
Response.ContentType = strType;
//Write the file content
this.Response.BinaryWrite(datas);
this.Response.End();
return new FileStreamResult(Response.OutputStream, obj.FType);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.