2

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



    }
}

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?

2 Answers 2

2

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

    } 
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

3 Comments

Thank you for you response sir.I already used that sir,so i'm getting error at:file.InputStream.Read(fileData, 0, (int)file.InputStream.Length); as---"'D:\Tiru\Projects\ProTrak\App_Data\RadUploadTemp\0ngsv0wx.fxs'."
one way to avoid the error is to comment this line radAsyncUpload.UploadedFiles[0].SaveAs(Server.MapPath("Uploads/") + path);
yes sir.But i want to save it in Uploads also,so how to proceed?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.