Hello.
I'm trying to save files in DB, which should record the file as "BinaryData".
I'm using RadAsyncUpload, but when i upload the file i get the extension "System.IO.IOException: The process cannot access the file because it is being used by another process." and the file only gets loaded in the App_Data folder of my app, in an automatically created "RadUploadTemp" folder.
I've tried to disable the Path mentions in my SaveFile method, getting "Path not found error". Any of the available telerik demos is similar to my context.
How do i solve this?
//my insert/update operations 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 (!string.IsNullOrEmpty(Ficheiro) && FileTipo != null)
{
model.Ficheiro = Path.GetFileNameWithoutExtension(Ficheiro);
model.FileContent = fileBytes;
model.FileTipo = Path.GetExtension(FileTipo);
}
if (!string.IsNullOrEmpty(FileID.Text) && Convert.ToInt32(FileID.Text) > 0)
{
model.ID = Convert.ToInt32(FileID.Text);
listagembll.UpdateFile(model);
}
else
{
listagembll.InsertFile(model);
}
//Ascx.Cs method
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
}
//Ascx reference
<telerik:RadAsyncUpload ID="RadAsyncUpload1" runat="server" AllowedFileExtensions="xlsx,xlsm,xls,txt,pdf" EditFormColumnIndex="1" MultipleFileSelection="Disabled" RenderMode="Lightweight" TargetFolder="" OnFileUploaded="RadAsyncUpload1_FileUploaded" UploadedFilesRendering="BelowFileInput" >
</telerik:RadAsyncUpload>
<br />
<span class="allowed-attachments">Formatos permitidos: <span class="allowed-attachments-list">pdf,xlsx,xlsm,xls,txt</span></span> </td>
EDIT: The error was resolved adding the use of "fileStream.Close();", nevertheless, now the file is saving as null, saving only the file name, and only allows 1 operations, disapeering on the second one.
I created a target folder. dofferent from the temporary folder. Being the file name saved, i suppose my logic shoyld be close to be right.
//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[] fileBytes = new byte[fileStream.Length - 1 + 1]; fileStream.Read(fileBytes, 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> </td>