如何使用AsyncFileUpload上传文件并将字节保存在数据库中?

时间:2011-08-25 08:12:06

标签: c# asp.net file-upload rad-controls

我正在使用radAsyncFileUpload上传文件。我想将文件内容保存为数据库中的字节。到目前为止,这是我的代码:

protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
    if (DetailsView1.CurrentMode == DetailsViewMode.Insert)
    {
        if (Session["ProjectId"] != null)
        {
            int Projectid = Convert.ToInt32(Session["ProjectId"]);
            string Description = (DetailsView1.FindControl("RadEditor1") as RadEditor).Content;
            RadAsyncUpload radAsyncUpload = DetailsView1.FindControl("RadAsyncUpload1") as RadAsyncUpload;

            UploadedFile file = radAsyncUpload.UploadedFiles[0];
            string s = file.FileName;
            string path = System.IO.Path.GetFileName(s);

            radAsyncUpload.UploadedFiles[0].SaveAs(Server.MapPath("Uploads/") + path);
            string Contenttype = radAsyncUpload.UploadedFiles[0].ContentType;
            int fileSize = radAsyncUpload.UploadedFiles[0].ContentLength;
            float length = float.Parse(fileSize.ToString());
            byte[] fileData = new byte[file.InputStream.Length];
            file.InputStream.Read(fileData, 0, (int)file.InputStream.Length);


                    ProTrakEntities objEntity = new ProTrakEntities();
                    ProjectFile objFile = new ProjectFile();
                    objFile.ProjectId = Projectid;
                    objFile.FileName = s;
                    objFile.FileType = Contenttype;
                    objFile.FileSize = length;
                    objFile.CreatedBy = "admin";
                    objFile.CreatedDate = DateTime.Now;
                    objFile.Description = Description;
                    objFile.FileData = fileData;
                    objEntity.AddToProjectFiles(objFile);
                    objEntity.SaveChanges();

        }
        DetailsView1.ChangeMode(DetailsViewMode.ReadOnly);
        ClientScript.RegisterStartupScript(Page.GetType(), "mykey", "CloseAndRebind('navigateToInserted');", true);



    }
}

我在第file.InputStream.Read(fileData, 0, (int)file.InputStream.Length);行上传时遇到错误:

  

Could not find file 'D:\Tiru\Projects\ProTrak\App_Data\RadUploadTemp\0ngsv0wx.fxs'.

这个错误是什么意思,我怎么能摆脱它?

2 个答案:

答案 0 :(得分:2)

由于没有编辑权.. em发布您的代码几乎没有修改,看看它是否有效.. 我的代码

protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e) 
{ 
    if (DetailsView1.CurrentMode == DetailsViewMode.Insert) 
    { 
        if (Session["ProjectId"] != null) 
        { 
            int Projectid = Convert.ToInt32(Session["ProjectId"]); 
            string Description = (DetailsView1.FindControl("RadEditor1") as RadEditor).Content; 
            RadAsyncUpload radAsyncUpload = DetailsView1.FindControl("RadAsyncUpload1") as RadAsyncUpload; 

            UploadedFile file = radAsyncUpload.UploadedFiles[0]; 
            string s = file.FileName; 
            string path = System.IO.Path.GetFileName(s); 


            string Contenttype = radAsyncUpload.UploadedFiles[0].ContentType; 
            int fileSize = radAsyncUpload.UploadedFiles[0].ContentLength; 
            float length = float.Parse(fileSize.ToString()); 
            byte[] fileData = new byte[file.InputStream.Length]; 
            file.InputStream.Read(fileData, 0, (int)file.InputStream.Length); 


                    ProTrakEntities objEntity = new ProTrakEntities(); 
                    ProjectFile objFile = new ProjectFile(); 
                    objFile.ProjectId = Projectid; 
                    objFile.FileName = s; 
                    objFile.FileType = Contenttype; 
                    objFile.FileSize = length; 
                    objFile.CreatedBy = "admin"; 
                    objFile.CreatedDate = DateTime.Now; 
                    objFile.Description = Description; 
                    objFile.FileData = fileData; 
                    objEntity.AddToProjectFiles(objFile); 
                    objEntity.SaveChanges(); 

        } 
        DetailsView1.ChangeMode(DetailsViewMode.ReadOnly); 
        ClientScript.RegisterStartupScript(Page.GetType(), "mykey", "CloseAndRebind('navigateToInserted');", true); 

radAsyncUpload.UploadedFiles[0].SaveAs(Server.MapPath("Uploads/") + path); 

    } 
}

答案 1 :(得分:1)

像这样使用..

RadAsyncUpload radAsyncUpload = InsertItem.FindControl("AsyncUpload1") as RadAsyncUpload;
        UploadedFile file = radAsyncUpload.UploadedFiles[0];      
        byte[] fileData = new byte[file.InputStream.Length];
        file.InputStream.Read(fileData, 0, (int)file.InputStream.Length);