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



我正在使用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:TiruProjectsProTrakApp_DataRadUploadTempngsv0wx.fxs'.

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

因为他们没有编辑权限。张贴你的代码,稍加修改,看看它是否有效。我的代码

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); 
    } 
}

像这样使用…

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);

相关内容

  • 没有找到相关文章

最新更新