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>
 
What is null? e.File.InputStream ?
 
The filebytes on line 8 of post number 15 is a local variable. It is different from the class variable filebytes on line 8 of post #1.
 
Coping the bytes to the variable fileBytes was the soluiton!
But yet another problem now, cant´t can´t replace file in DB. I can only load if the DB register is empty. Any ideas on that?
 
I don't know. All you are showing us in post #1 is a call to UpdateFile(). Perhaps post that code so that we are not guessing.

If that code is actually doing an insert rather than an update, well, there you have it. If it's truly doing an update, how is the initial row created so that you can update it? If it's doing an upsert in theory it should just work.
 
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)
            {
            }
        }
 
What error you getting?
 
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?
 
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
 
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.
 
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.
 
Back
Top Bottom