将图片上传到数据库 因File.Open遭遇System.UnauthorizedAccessException

本人在尝试 上传图片到服务器数据库中以Image类型进行保存时
其初只在本机进行操作
利用如下代码 进行图片到byte[]后 保存到数据库
using (SqlConnection sqlConn = new SqlConnection(DBClass.DBConnString()))
{
    sqlConn.Open();
    FileStream fs = File.Open(this.FileUpload1.PostedFile.FileName, FileMode.Open);
    BinaryReader br = new BinaryReader(fs);
    byte[] photo = br.ReadBytes((int)fs.Length);
    br.Close();
    fs.Close();

    String strSql = "INSERT INTO PhotoInfo( AlbumID, PhotoName, PhotoType ";
    strSql += " , PhotoContent, PhotoByteSize, PhotoNote )";
    strSql += " VALUES( '" + Request.QueryString["AlbumID"].Trim() + "' ";
    strSql += " ,'" + this.txt_PhotoName.Text.Trim() + "' ";
    strSql += " ,'" + this.FileUpload1.PostedFile.ContentType + "' ";
    strSql += " ,@photoBinary ";
    strSql += " ," + this.FileUpload1.PostedFile.ContentLength + " ";
    strSql += " ,'" + this.txt_PhotoNote.Text.Trim() + "' ";
    strSql += " ) ";

    SqlCommand sqlComm = new SqlCommand(strSql, sqlConn);
    sqlComm.Parameters.Add("@photoBinary", SqlDbType.Image, photo.Length);
    sqlComm.Parameters["@photoBinary"].Value = photo;

    sqlComm.ExecuteNonQuery();
    sqlConn.Close();
}
在本机操作没有问题
但其他用户从其他电脑进行图片上传时 就出现了如下错误
-------------------------------------------------------
对路径“C:\Documents and Settings\Administrator\桌面\logo.jpg”的访问被拒绝。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.UnauthorizedAccessException: 对路径“C:\Documents and Settings\Administrator\桌面\logo.jpg”的访问被拒绝。

ASP.NET 未被授权访问所请求的资源。请考虑授予 ASP.NET 请求标识访问此资源的权限。ASP.NET 有一个在应用程序没有模拟时使用的基进程标识(通常,在 IIS 5 上为 {MACHINE}\ASPNET,在 IIS 6 上为网络服务)。如果应用程序正在通过 <identity impersonate="true"/> 模拟,则标识将为匿名用户(通常为 IUSR_MACHINENAME)或经过身份验证的请求用户。

要将 ASP.NET 访问权限授予某个文件,请在资源管理器中右击该文件,选择“属性”,然后选择“安全”选项卡。单击“添加”添加适当的用户或组。突出显示 ASP.NET 帐户,选中所需访问权限对应的框。

源错误:


行 38:             //FileStream fs = new System.IO.FileStream(this.FileUpload1.PostedFile.FileName, FileMode.Open, FileAccess.Read);
行 39:
行 40:             FileStream fs = File.Open(this.FileUpload1.PostedFile.FileName, FileMode.Open);
行 41:             BinaryReader br = new BinaryReader(fs);
行 42:             byte[] photo = br.ReadBytes((int)fs.Length);
 .......
=======================================
经多方查找及尝试 问题出在File.Open上
所以修改上传图片到数据库的代码如下:
using (SqlConnection sqlConn = new SqlConnection(DBClass.DBConnString()))
{
    sqlConn.Open();
    int intImageSize;
    string strImageType;
    Stream ImageStream;

    intImageSize = this.FileUpload1.PostedFile.ContentLength;
    ImageStream = this.FileUpload1.PostedFile.InputStream;
    strImageType = this.FileUpload1.PostedFile.ContentType;
    Byte[] ImageContent = new Byte[intImageSize];
    int intStatus;
    intStatus = ImageStream.Read(ImageContent, 0, intImageSize);

    String strSql = "INSERT INTO PhotoInfo( AlbumID, PhotoName, PhotoType ";
    strSql += " , PhotoContent, PhotoByteSize, PhotoNote )";
    strSql += " VALUES( '" + Request.QueryString["AlbumID"].Trim() + "' ";
    strSql += " ,'" + this.txt_PhotoName.Text.Trim() + "' ";
    strSql += " ,'" + this.FileUpload1.PostedFile.ContentType + "' ";
    strSql += " ,@photoBinary ";
    strSql += " ," + this.FileUpload1.PostedFile.ContentLength + " ";
    strSql += " ,'" + this.txt_PhotoNote.Text.Trim() + "' ";
    strSql += " ) ";

    SqlCommand sqlComm = new SqlCommand(strSql, sqlConn);
    sqlComm.Parameters.Add("@photoBinary", SqlDbType.Image, intImageSize);
    sqlComm.Parameters["@photoBinary"].Value = ImageContent;

    sqlComm.ExecuteNonQuery();
    sqlConn.Close();
}

posted on 2008-03-07 14:51  freeliver54  阅读(1368)  评论(0编辑  收藏  举报

导航