File transfer eating alot of CPU
Posted
by
Dan C.
on Stack Overflow
See other posts from Stack Overflow
or by Dan C.
Published on 2012-06-18T20:39:52Z
Indexed on
2012/06/18
21:16 UTC
Read the original article
Hit count: 358
I'm trying to transfer a file over a IHttpHandler, the code is pretty simple. However when i start a single transfer it uses about 20% of the CPU. If i were to scale this to 20 simultaneous transfers the CPU is very high. Is there a better way I can be doing this to keep the CPU lower? the client code just sends over chunks of the file 64KB at a time.
public void ProcessRequest(HttpContext context)
{
if (context.Request.Params["secretKey"] == null)
{
}
else
{
accessCode = context.Request.Params["secretKey"].ToString();
}
if (accessCode == "test")
{
string fileName = context.Request.Params["fileName"].ToString();
byte[] buffer = Convert.FromBase64String(context.Request.Form["data"]);
string fileGuid = context.Request.Params["smGuid"].ToString();
string user = context.Request.Params["user"].ToString();
SaveFile(fileName, buffer, user);
}
}
public void SaveFile(string fileName, byte[] buffer, string user)
{
string DirPath = @"E:\Filestorage\" + user + @"\";
if (!Directory.Exists(DirPath))
{
Directory.CreateDirectory(DirPath);
}
string FilePath = @"E:\Filestorage\" + user + @"\" + fileName;
FileStream writer = new FileStream(FilePath, File.Exists(FilePath) ? FileMode.Append : FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
writer.Write(buffer, 0, buffer.Length);
writer.Close();
}
© Stack Overflow or respective owner