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>
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,488
Location
Chesapeake, VA
Programming Experience
10+

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,488
Location
Chesapeake, VA
Programming Experience
10+
As for your content, you are reading your data into dados, but not pointing filebytes to dados.
 

harlem98

Active member
Joined
Nov 4, 2021
Messages
27
Programming Experience
Beginner
If you look at the Telerik documentation, e.File.ContentType in the MIME type, not the file name extension.

Thank you, i haven't notice that. You know if it is possible to retrieve just the short extension? like PDF ou xlsx?
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,488
Location
Chesapeake, VA
Programming Experience
10+
You're right but i cannot bind it in the Save method
Use filebytes instead of the local dados in RadAsyncUpload1_FileUploaded().
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,488
Location
Chesapeake, VA
Programming Experience
10+
Well, duh... Stupid me. I hate this memory loss. If your Save() is called on a different post back from the user, then you are dealing with a brand new page instance. Read up on the ASP.NET page life cycle and strategies for caching information.
 

harlem98

Active member
Joined
Nov 4, 2021
Messages
27
Programming Experience
Beginner
Well, duh... Stupid me. I hate this memory loss. If your Save() is called on a different post back from the user, then you are dealing with a brand new page instance. Read up on the ASP.NET page life cycle and strategies for caching information.
My save method is triggered in the save button of the upload, so i believe that's not the issue. I've tried your suggestion with any results. Another "cheating approach" i've made was trying to convert the file to byte[] directly in the save method, the following way.
C#:
model.FileContent = Encoding.ASCII.GetBytes(Server.MapPath("~/TargetFiles"));
And it does save the file at the server, in the format of "file", because it is obviously missing the uploaded file reference, nevertheless, i cannot find a way to add the reference. Is it possible to add it through this approach?
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,488
Location
Chesapeake, VA
Programming Experience
10+
Are you saying that clicking on the Save button starts the upload? If so the the File upload object should be available on that postback.

If not, the when your get the upload save it to your upload location, and retain the filename of the saved file either the cache or some hidden label on the form. When the postback for the save eventually happens, get the the filename from the cache or hidden label so that you can get the file contents.
 

harlem98

Active member
Joined
Nov 4, 2021
Messages
27
Programming Experience
Beginner
Are you saying that clicking on the Save button starts the upload? If so the the File upload object should be available on that postback.

If not, the when your get the upload save it to your upload location, and retain the filename of the saved file either the cache or some hidden label on the form. When the postback for the save eventually happens, get the the filename from the cache or hidden label so that you can get the file contents.
Telerik fileUpload does preciselly that from it´s construction. By theory i've done everything ok, it should be some mistake in the code
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,488
Location
Chesapeake, VA
Programming Experience
10+
Telerik fileUpload does preciselly that from it´s construction.
Does which one? The single postback or the two postbacks?
 

harlem98

Active member
Joined
Nov 4, 2021
Messages
27
Programming Experience
Beginner
Does which one? The single postback or the two postbacks?
the two postbacks, it performs an automatical one, when file is selected, saving the file on a designated, and in the second one we trigger the button into server. I really don't know, what could be missing or wrong on my code.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,488
Location
Chesapeake, VA
Programming Experience
10+
If there are two postbacks then the e.File.FileStream is likely invalid on the second post back by the time your save button is pressed.

All the code below is just give you the ASCII byte equivalent of the physical path to your "TargetFiles" folder.
C#:
model.FileContent = Encoding.ASCII.GetBytes(Server.MapPath("~/TargetFiles"));

You actually need to crack open the file of interest, read the contents into your a byte array (e.g. your filebytes) and pass that into your model's FileContent property.

As I said, to understand most of this, take time to read about and understand the ASP.NET page life cycle. It well help you understand what is happening when, and so you can debug better rather than just guessing.

 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,488
Location
Chesapeake, VA
Programming Experience
10+

harlem98

Active member
Joined
Nov 4, 2021
Messages
27
Programming Experience
Beginner
If there are two postbacks then the e.File.FileStream is likely invalid on the second post back by the time your save button is pressed.

All the code below is just give you the ASCII byte equivalent of the physical path to your "TargetFiles" folder.
C#:
model.FileContent = Encoding.ASCII.GetBytes(Server.MapPath("~/TargetFiles"));

You actually need to crack open the file of interest, read the contents into your a byte array (e.g. your filebytes) and pass that into your model's FileContent property.

As I said, to understand most of this, take time to read about and understand the ASP.NET page life cycle. It well help you understand what is happening when, and so you can debug better rather than just guessing.

I tried to do preciselly that, still getting null althaugh.
C#:
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[] fileBytes = new byte[fileStream.Length - 1 + 1];
            fileStream.Read(fileBytes, 0, System.Convert.ToInt32(fileStream.Length));
            fileStream.Close();
        }

//save method
//model.FileContent = fileBytes;
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,488
Location
Chesapeake, VA
Programming Experience
10+
What is null? e.File.InputStream ?
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,488
Location
Chesapeake, VA
Programming Experience
10+
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.
 

harlem98

Active member
Joined
Nov 4, 2021
Messages
27
Programming Experience
Beginner
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?
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,488
Location
Chesapeake, VA
Programming Experience
10+
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.
 
Top Bottom