开发者

How can I upload a file using AsyncFileUpload and save the bytes in database?

I'm using radAsyncFileUpload to upload a file. I want to save the file contents in a database as bytes. Here's my code so far:

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;
                    ob开发者_如何学PythonjFile.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);



    }
}

I'm getting an error while uploading at the line file.InputStream.Read(fileData, 0, (int)file.InputStream.Length);:

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

What does this error mean, and how can I get rid of it?


As em not having the edit right.. em posting ur code with little modification see if it works.. my code

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

    } 
}


use like this..

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜