Search code examples
c#file-uploadtelerik

File upload saving the folder instead of the file


I'm performing upload of files into DB with Telerik's ASP.NET RadAsyncpload, which is very similar to the normal fileUpload.

My problem is that, when using the variable to store the Data, it saves as "null". in alternative i tried to use Server.MapPath, which is for is turn saving the folder localtion instead of the File.

What am i doing wrong?

//partial class declarations
   (...)
   string Ficheiro = string.Empty;
   string FileTipo = string.Empty;
   byte[] fileBytes = null;

//Save method, triggered by save button after upload
  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.FileName = Path.GetFileNameWithoutExtension(Ficheiro); //FileName
                model.FileTipo = Path.GetExtension(FileTipo); //FileExtension
                model.FileContent = fileBytes; //Content null
                model.FileContent = Encoding.ASCII.GetBytes(HttpContext.Current.Server.MapPath("~/TargetFiles/ + model.FileName")); //Content saved is location of the folder

//Upload method
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(dados, 0, System.Convert.ToInt32(fileStream.Length));
        fileStream.Close();
    }

Solution

  • RESOLVED! I was missing copy the stream to the variable fileBytes.

    public void RadAsyncUpload1_FileUploaded(object sender, FileUploadedEventArgs e)
        {
                RadAsyncUpload1.Visible = false; //false
                var liItem = new HtmlGenericControl("li");
                Ficheiro = e.File.FileName; // sintaxe metodo
                FileTipo = e.File.ContentType;
                e.IsValid = true;
                e.File.InputStream.Position = 0;
                fileBytes = new byte[e.File.InputStream.Length];
                for (int totalBytesCopied = 0; totalBytesCopied < e.File.InputStream.Length; )
                    totalBytesCopied += e.File.InputStream.Read(fileBytes, totalBytesCopied, Convert.ToInt32(e.File.InputStream.Length) - totalBytesCopied); //conversao para bytes
                e.File.InputStream.Close();
    
                if (File.Exists("~/App_Data/RadUploadTemp"))
                {
                    e.IsValid = false;
                    e.File.SaveAs(Server.MapPath(
                        Path.Combine(RadAsyncUpload1.TargetFolder, e.File.GetNameWithoutExtension() + "1" + e.File.GetExtension())));
                }
        }