方法一:这种方法是可以正常保存图片
HttpPostedFile oFile = HttpContext.Current.Request.Files[HttpContext.Current.Request.Files.AllKeys[0]];
oFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath("~") + sFileName);//上传图片
方法二:这种方法保存的图片是无法打开的.因为要用到webservice上传.需要传递流或者是二进制.传流过去的上传也无法正常保存图片.
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]];
oFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath("~") + sFileName);//上传图片
方法二:这种方法保存的图片是无法打开的.因为要用到webservice上传.需要传递流或者是二进制.传流过去的上传也无法正常保存图片.
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(); }求解..谢谢.
System.IO.Stream fs;
fs = (System.IO.Stream)oFile.InputStream;
fs.Read(buffer, 0, oFile.ContentLength)
WebService1.UploadFile(b, FileName);
fs.Close();
一下这句不用强转就是 System.IO.Stream 类型吧?
fs = (System.IO.Stream)oFile.InputStream;
嗯.但是上传的图片就是错误的.无法打开.求解
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;
}
}
可能是因为WS 传输的时候,数据流被截断了
没有完全传过去