Upload in DB

harlem98

Active member
Joined
Nov 4, 2021
Messages
27
Programming Experience
Beginner
Hello community. I'm using Telerik ASP.NET Ajax's RadAsyncUpload in order to upload in DataBase. At the moment, i got some problems; it only allows one upload operations for postback, blocking after the first one. It´s saving FileContent as null, and FileExtension (FileTipo in my code) as blank.

Could you help me fix it?

C#:
//partial class declarations

(...)

        string Ficheiro = string.Empty;
        string FileTipo = string.Empty;

       byte[] fileBytes = null;

(...)

//DataBase save method
public void SaveFile(object sender, EventArgs e)
        {
            ListagemTimesheet model = new ListagemTimesheet();
          
            model.IDRecursoHumano = Convert.ToInt32(rdpInvestigadorE.Text);
            model.IDEstadoTimesheet = Convert.ToInt32(rcbEstado.SelectedValue);
            model.Observações = Obervaçoestxt.Text;
            model.AssinaturaTimesheet = txtAssinaturaTimesheet.Text;
            model.DataEnvio = DataEnvio.SelectedDate.Value;

            if (Objecto.ID > 0)
            {
                model.ID = Convert.ToInt32(FileID.Text);

                if (!string.IsNullOrEmpty(Ficheiro) && FileTipo != null)
                {

                    model.Ficheiro = Path.GetFileNameWithoutExtension(Ficheiro); //FileName
                    model.FileTipo = Path.GetExtension(FileTipo); //FileExtension
                    model.FileContent = fileBytes; //Content
                }

                if (!string.IsNullOrEmpty(FileID.Text) && Convert.ToInt32(FileID.Text) > 0)
                {
                    model.ID = Convert.ToInt32(FileID.Text);
                    listagembll.UpdateFile(model);

                }
                else
                {
 
                }
            }

//Code-behind
protected void Page_Load(object sender, EventArgs e)
        {
            RadAsyncUpload1.TargetFolder = Server.MapPath("~/TargetFiles");
        }

public void RadAsyncUpload1_FileUploaded(object sender, FileUploadedEventArgs e)
        {
            RadAsyncUpload1.Visible = false;
            Stream fileStream = e.File.InputStream;
            Ficheiro = e.File.FileName; // sintaxe metodo
            FileTipo = e.File.ContentType;
            e.IsValid = true;
            byte[] dados = new byte[fileStream.Length - 1 + 1];
            fileStream.Read(dados, 0, System.Convert.ToInt32(fileStream.Length));
            fileStream.Close();
        }
//FrontEnd
<telerik:RadAsyncUpload ID="RadAsyncUpload1" runat="server" AllowedFileExtensions="xlsx,xlsm,xls,txt,pdf" MultipleFileSelection="Disabled"  OverwriteExistingFiles="true" OnFileUploaded="RadAsyncUpload1_FileUploaded" UploadedFilesRendering="BelowFileInput" Culture="pt-PT" TemporaryFileExpiration="00:30:00" ToolTip="Anexar ficheiro"></telerik:RadAsyncUpload><span class="allowed-attachments">Formatos permitidos: <span class="allowed-attachments-list">pdf,xlsx,xlsm,xls,txt</span></span>&nbsp;</td>
 

harlem98

Active member
Joined
Nov 4, 2021
Messages
27
Programming Experience
Beginner
weird.. i got
C#:
public int InsertFile(ListagemTimesheet model)
        {
            try
            {
                using (model lt = new model())
                {
                    lt.ListagemTimesheets.AddObject(model);
                    lt.SaveChanges();
                    return model.ID;
                }
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        public void UpdateFile(ListagemTimesheet model)
        {
            try
            {
                using (model lt = new model())
                {
                    var result = lt.ListagemTimesheets.SingleOrDefault(b => b.ID == model.ID);
                    if (result != null)
                    {

                        result.ID = model.ID;
                        result.IDRecursoHumano = model.IDRecursoHumano;
                        result.IDEstadoTimesheet = model.IDEstadoTimesheet;
                        result.FileContent = model.FileContent;
                        result.Ficheiro = model.Ficheiro;
                        result.DataEnvio = model.DataEnvio;
                        result.FileTipo = model.FileTipo;
                        result.AssinaturaTimesheet = model.AssinaturaTimesheet;
                        result.Observações = model.Observações;
                        lt.SaveChanges();
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,488
Location
Chesapeake, VA
Programming Experience
10+
What error you getting?
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,488
Location
Chesapeake, VA
Programming Experience
10+
On which line of code?
Does it indicate which file is being accessed?

How are you loading file data? Are you sure you are closing the stream to the file?
 

harlem98

Active member
Joined
Nov 4, 2021
Messages
27
Programming Experience
Beginner
On which line of code?
Does it indicate which file is being accessed?

How are you loading file data? Are you sure you are closing the stream to the file?
The asyncupload method, which handles the file concludes, and the exception is throw in the end of the method. It doesen't overrite the stored file. For my searching there is no indication that this is normal, it should'n evaluate the old file. There is something missing me obviously, but i cant find what it is normal. When there is a file, the process chain doesen't reach the save file
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,488
Location
Chesapeake, VA
Programming Experience
10+
Sorry can't help you. I don't have a Telerik license anymore. You may get more help with Telerik's technical support, or any Telerik specific forums.
 

harlem98

Active member
Joined
Nov 4, 2021
Messages
27
Programming Experience
Beginner
Sorry can't help you. I don't have a Telerik license anymore. You may get more help with Telerik's technical support, or any Telerik specific forums.
Thank you for the effort! I'm "half happy" because at least i was able to insert on empty registers.
 
Top Bottom