The process cannot access the file because it is being used by another process.

2 Answers 6585 Views
AsyncUpload
Harlem98
Top achievements
Rank 1
Iron
Iron
Iron
Harlem98 asked on 07 Dec 2021, 04:52 PM

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> &nbsp; </td>


 

Harlem98
Top achievements
Rank 1
Iron
Iron
Iron
commented on 09 Dec 2021, 04:52 PM | edited

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>&nbsp;</td>


2 Answers, 1 is accepted

Sort by
0
Peter Milchev
Telerik team
answered on 10 Dec 2021, 01:38 PM

Hello Rúben,

You can check the following resources related to the access of the temporary files:

This should help you access the file itself, save it in the database, and if you do not set a Target folder, the file will be automatically removed from the Temporary folder.

If the file is still used by another process, you can check which process is locking it as there is a chance a Firewall or an Antivirus software can quarantine the files in the Temp folder.

Regards,
Peter Milchev
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Harlem98
Top achievements
Rank 1
Iron
Iron
Iron
commented on 10 Dec 2021, 02:22 PM

Hello Peter.

I try to use a similar approach to the Demo made with OleDB, with input stream, assigning the filebytes property. Nevertheless, i get null value in the DB.

What is missing me? I'm using TargetFolder, and it is successfully getting FileName.


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, triggered by save button after upload is made
(...)
model.FileContent = fileBytes; //Saves Null

0
Harlem98
Top achievements
Rank 1
Iron
Iron
Iron
answered on 10 Dec 2021, 04:13 PM | edited on 14 Dec 2021, 06:31 PM

Partially resolved it! Hope this turns usefull. I was missing to copy the bytes to the variable. i'm still with problems nevertheless

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();
        }

//Save
(...)
 model.FileContent = fileBytes;

I can only save file if DB register is empty. How to add the possibility of replacing the file to my code? When i try to edit a file, it still throws the same ""The process cannot access the file because it is being used by another process.". Or it is not allowed?

And another problem is that i am using the RadAsyncUpload on a window, so i can only perform one operation until a complete page reload. Is it possible to use a partial postback in the window?

Peter Milchev
Telerik team
commented on 15 Dec 2021, 01:47 PM

If you manage to get the uploaded file as a byte array, I believe there is nothing related to the AsyncUpload anymore because you have a copy of that file. With that said, probably the error is related to the DB itself? 

It is possible to see this error if you try to modify the e.File.InputStream instead of updating the database.

Harlem98
Top achievements
Rank 1
Iron
Iron
Iron
commented on 15 Dec 2021, 02:42 PM

I managed to bounce off the issue by not binding the file to edit form. Displaying the item as empty in the form, allows me to update a new file normally. Not the ideal scenario but allows me to persuit.

I'm having other problems using this implementation along RadWindow, as well so it could be something wrong in my code or it is not possible to display the file value in edit form?

Peter Milchev
Telerik team
commented on 20 Dec 2021, 01:38 PM | edited

It depends on your whole setup, there might be some configuration issues.

You can also try the built-in capabilities of the Grid with popup editor and GridAttachment column instead of implementing it all from scratch.

As for the AsyncUpload, you can have the file only when selecting the file, then, once it is uploaded, you can access it on the server before it is moved to the Target folder. Once that is done, the AsyncUpload cannot access the files anymore and you need custom code to load a file from the DB.

Another thing you might see as an issue is that the file is processed on the first postback, so if your other editors do postback, that would result in a file loss. You can check this article how to overcome this behavior: https://docs.telerik.com/devtools/aspnet-ajax/controls/asyncupload/how-to/how-to-persist-uploaded-files 

Tags
AsyncUpload
Asked by
Harlem98
Top achievements
Rank 1
Iron
Iron
Iron
Answers by
Peter Milchev
Telerik team
Harlem98
Top achievements
Rank 1
Iron
Iron
Iron
Share this question
or