fileinnfo的错误代码
FileInfo File = new FileInfo(Server.MapPath("/Content/ExcelFiles/SalesTarget.xlsx"));
if (File != null)
{
int Size = Convert.ToInt32(File.Length);
string FileName = File.FullName;
int Position = FileName.LastIndexOf("\\");
FileName = FileName.Substring(Position + 1);
string test = File.GetType().ToString();
string filepath = Server.MapPath("/Content/ExcelFiles/SalesTarget.xlsx");
string ContentType = MimeMapping.GetMimeMapping(Server.MapPath("/Content/ExcelFiles/SalesTarget.xlsx"));
byte[] FileData = new byte[Size];
char[] chars = Encoding.Unicode.GetChars(FileData);
StreamReader reader = new StreamReader(filepath);
reader.Read(chars, 0, Size); // Throwing error here
Response.ContentType = ContentType;
StringBuilder SB = new StringBuilder();
SB.Append("filename=");
SB.Append(FileName);
Response.AddHeader("Content-Disposition", SB.ToString());
Response.BinaryWrite(FileData);
Response.Flush();
Response.End();
//File..Read(FileData, 0, Size);
}
使用Httppostedfile类运行代码:
HttpPostedFileBase File = Request.Files["FileToLoad"];
if (File != null)
{
int Size = File.ContentLength;
if (Size <= 0)
{
ViewData["ERROR"] = "You uploaded an empty file,please browse a valid file to upload";
return View("../Shared/Error");
}
string FileName = File.FileName;
int Position = FileName.LastIndexOf("\\");
FileName = FileName.Substring(Position + 1);
string ContentType = File.ContentType;
byte[] FileData = new byte[Size];
File.InputStream.Read(FileData, 0, Size);
theModel.AddAFile(FileName, Size, ContentType, FileData);
}
我想从目录下载现有文件而不是使用httppostedfile,所以我在读取文件数据时遇到错误。