将InputStream转换为byte[].上传的就是损坏的...

ChinaUncle-- 2012-07-17 01:12:46
方法一:这种方法是可以正常保存图片
	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();

}


求解..谢谢.
...全文
690 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ChinaUncle-- 2012-07-20
  • 打赏
  • 举报
回复
结贴..最终使用的是将流转为string传递.
凤凰涅檠 2012-07-18
  • 打赏
  • 举报
回复
如果说WS上传的就是损坏的
可能是因为WS 传输的时候,数据流被截断了
没有完全传过去
ChinaUncle-- 2012-07-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

InputStream的类型就是System.IO.Stream类型的,其实不用转
http://msdn.microsoft.com/zh-cn/library/system.web.httppostedfile.inputstream.aspx
[/Quote]

代码不报错.没有异常.
只是生成的图片是错误的.无法打开的.
ChinaUncle-- 2012-07-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
InputStream的类型就是System.IO.Stream类型的,其实不用转
http://msdn.microsoft.com/zh-cn/library/system.web.httppostedfile.inputstream.aspx
[/Quote]

嗯.但是上传的图片就是错误的.无法打开.求解

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;
}
}
孟子E章 2012-07-17
  • 打赏
  • 举报
回复
InputStream的类型就是System.IO.Stream类型的,其实不用转
http://msdn.microsoft.com/zh-cn/library/system.web.httppostedfile.inputstream.aspx
ChinaUncle-- 2012-07-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

byte[] buffer = new byte[oFile.ContentLength];
System.IO.Stream fs;
fs = (System.IO.Stream)oFile.InputStream;
fs.Read(buffer, 0, oFile.ContentLength)
WebService1.UploadFile(b, FileName);
fs.C……
[/Quote]

一下这句不用强转就是 System.IO.Stream 类型吧?
fs = (System.IO.Stream)oFile.InputStream; 
孟子E章 2012-07-17
  • 打赏
  • 举报
回复
byte[] buffer = new byte[oFile.ContentLength];
System.IO.Stream fs;
fs = (System.IO.Stream)oFile.InputStream;
fs.Read(buffer, 0, oFile.ContentLength)
WebService1.UploadFile(b, FileName);
fs.Close();

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧