62,046
社区成员
发帖
与我相关
我的任务
分享
HttpPostedFile oFile = HttpContext.Current.Request.Files[HttpContext.Current.Request.Files.AllKeys[0]];
oFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath("~") + sFileName);//上传图片
HttpPostedFile oFile = HttpContext.Current.Request.Files[HttpContext.Current.Request.Files.AllKeys[0]];
using (FileStream fs = new FileStream(sFilePath, FileMode.Create, FileAccess.Write, FileShare.None))
{
const int bufferLen = 4096;
byte[] buffer = new byte[bufferLen];
int count = 0;
while ((count = oFile.InputStream.Read(buffer, 0, bufferLen)) > 0)
{
fs.Write(buffer, 0, count);
}
fs.Close();
oFile.InputStream.Close();
}
HttpPostedFile oFile = HttpContext.Current.Request.Files[HttpContext.Current.Request.Files.AllKeys[0]];
byte[] buffer = new byte[oFile.ContentLength];
System.IO.Stream fs;
fs = (System.IO.Stream)oFile.InputStream;
fs.Read(buffer, 0, oFile.ContentLength);
UploadFile(buffer, sServerDir, sFileName);
public bool UploadFile(byte[] fs, string SavePath, string FileName)
{
// string path = System.Configuration.ConfigurationSettings.AppSettings["PicPath"].ToString();
try
{
//判断类型
string picName = FileName;
//string fileType = "";
//fileType = picName.Substring(picName.LastIndexOf("."), picName.Length - picName.LastIndexOf(".")).ToLower();
//if (fileType != ".jpg" && fileType != ".gif" && fileType != ".bmp")
//{
// return false;
//}
string fullPath = System.AppDomain.CurrentDomain.BaseDirectory + SavePath;
if (!System.IO.Directory.Exists(fullPath))
System.IO.Directory.CreateDirectory(fullPath);
///定义并实例化一个内存流,以存放提交上来的字节数组。
MemoryStream m = new MemoryStream(fs);
///定义实际文件对象,保存上载的文件。
FileStream f = new FileStream(fullPath + FileName, FileMode.Create);
///把内内存里的数据写入物理文件
m.WriteTo(f);
m.Close();
f.Close();
f = null;
m = null;
// DirAppend.WriteLogFile("上传完成" + fullPath + FileName);
return true;
}
catch (Exception ex)
{
// DirAppend.WriteLogFile(ex.ToString());
return false;
}
}
fs = (System.IO.Stream)oFile.InputStream;