StreamObject.Read 无法访问已关闭的文件
asp.net 2005(vb)写的多个文件上传,调试没有问题,但是发布后出现问题。
代码如下
For Each HIF In hifB
Dim fn As String = System.IO.Path.GetFileName(HIF.PostedFile.FileName)
Dim sFileName As String
sFileName = HIF.PostedFile.FileName
sFileName = Right(sFileName, Len(sFileName) - InStrRev(sFileName, "\"))
Dim FileLength = 0
'记录文件长度变量
FileLength = HIF.PostedFile.ContentLength
Dim StreamObject As System.IO.Stream = HIF.PostedFile.InputStream
'文件临时储存Byte数组
Dim FileByteArray(FileLength) As Byte
'读取图象文件数据,FileByteArray为数据储存体,0为数据指针位置、FileLnegth为数据长度
Dim x = HIF.PostedFile.FileName.LastIndexOf("\"), File_name = Mid(HIF.PostedFile.FileName, x + 2)
'读文件内容到FileByteArray数组
下边的语句出错误:
StreamObject.Read(FileByteArray, 0, FileLength)
HIF.PostedFile.SaveAs("c:\\images\\" & i & fn)
sSQL = "insert into sysMailAffix (MailID,SerialID,AffixName,Type,FSize,Affix) values (@MailID,@SerialID,@AffixName,@Type,@FSize,@Affix)"
objCom = New SqlClient.SqlCommand(sSQL, objCon)
objCom.Parameters.Add("@Affix", SqlDbType.Binary, FileLength).Value = FileByteArray
objCom.Parameters.Add("@mailid", SqlDbType.Int).Value = 233
objCom.Parameters.Add("@serialid", SqlDbType.Int).Value = i
objCom.Parameters.Add("@affixname", SqlDbType.NVarChar, 50).Value = strTo & sFileName
objCom.Parameters.Add("@type", SqlDbType.NVarChar, 50).Value = HIF.PostedFile.ContentType
objCom.Parameters.Add("@FSize", SqlDbType.Float).Value = FileLength
objCom.ExecuteScalar()
Next
---------------------------
错误信息:无法访问已关闭的文件
------解决方案--------------------懒的看,给个类吧
C# code
public class upfile
{
private string path = null;
private string fileType = null;
private int sizes = 0;
/// <summary>
/// 初始化变量
/// </summary>
public upfile()
{
path = @"Userpic\"; //上传路径
fileType = "jpg|gif|bmp";
sizes = 1000 * 1000; //传文件的大小,默认200KB
}
/// <summary>
/// 设置上传路径,如:uploadimages\
/// </summary>
public string Path
{
set
{
path = value;
}
}
/// <summary>
/// 设置上传文件大小,单位为KB
/// </summary>
public int Sizes
{
set
{
sizes = value * 1024;
}
}
/// <summary>
/// 设置上传文件的类型,如:jpg|gif|bmp
/// </summary>
public string FileType
{
set
{
fileType = value;
}
}
/// <summary>
/// 上传图片
/// </summary>
/// <param name="name">上传控件名称</param>
/// <param name="creatDirectory">true则以当前时间创建文件夹,false则为设置的文件夹</param>
/// <returns>返回上传图片的相对路径</returns>
public string fileSaveAs(System.Web.UI.WebControls.FileUpload name, bool creatDir