二进制流上传文件到服务器(部分)

public void Up()
{
UploadFile("jing",@"D:\11.xls");
}

public ActionResult UploadFile(string userID, string path)
{
Byte[] MeaningFile;
FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
int size = Convert.ToInt32(stream.Length);
MeaningFile = new Byte[size];
stream.Read(MeaningFile, 0, size);
stream.Close();
FileStream fos = null;

string fileName = "";
string fileExt = path.Substring(path.LastIndexOf("."));
if (fileExt == ".xls" || fileExt == ".xlsx")//必须是EXCEL文件
{
try
{
string filePath = Server.MapPath("~/DataFile/");
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
fileName = userID + fileExt;
fos = new FileStream(filePath + fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
fos.Write(MeaningFile, 0, MeaningFile.Length);
fos.Close();
}
catch
{
}
finally
{
if (fos != null)
{
try
{
fos.Close();
}
catch
{
}
}
}
}
else
{

}
return RedirectToAction("UpLoad");
}

你可能感兴趣的:(二进制流上传文件到服务器(部分))