This is a migrated thread and some comments may be shown as answers.

Need help with filenotfoundexception

9 Answers 160 Views
AsyncUpload
This is a migrated thread and some comments may be shown as answers.
Daniel
Top achievements
Rank 1
Daniel asked on 30 Oct 2013, 09:05 AM
Hello Community,

i use the Telerik radasyncupload control like this:

Web.Config
<appSettings>
  <add key="Telerik.AsyncUpload.TemporaryFolder" value="~/App_Data/RadUploadTemp" />
</appSettings>

asp.net
<telerik:RadAsyncUpload ID="rauIconUpload" runat="server" ChunkSize="0" Localization-Cancel="Löschen" Localization-Remove="Entfernen" Localization-Select="Auswählen"
  Culture="de-DE" Skin="MetroTouch" TargetFolder="img/icons" MaxFileInputsCount="1">
</telerik:RadAsyncUpload>
<telerik:RadButton ID="rbtnIconUpload" runat="server" Text="Speichern" Skin="MetroTouch"></telerik:RadButton>

vb.net
Private Sub rbtnIconUpload_Click(sender As Object, e As EventArgs) Handles rbtnIconUpload.Click
 
    If rtxtIconBezeichnung.Text = String.Empty Or rtxtIconBezeichnung.Text = Nothing Or CHKValidation(rtxtIconBezeichnung.Text) = False Then
        rnfUngueltigeEingabe.Visible = True
    Else
        Try
            For Each f As UploadedFile In rauIconUpload.UploadedFiles
                Dim img As New System.Drawing.Bitmap(f.InputStream)
                Dim h As Integer = img.Height
                Dim w As Integer = img.Width
                img.Dispose()
 
                Dim fileName As String = f.GetName()
                IconPfad = "~/img/icons/" & fileName
 
                If w = 16 And h = 16 Then
                    IconSize = "16x16"
                ElseIf w = 32 And h = 32 Then
                    IconSize = "32x32"
                Else
                    rnfIconNichtErzeugt.Visible = True
                    Exit For
                End If
 
                IconErzeugt = Datenzugriff.CRTNeuesIcon(rtxtIconBezeichnung.Text, IconPfad, rcbIconGruppe.SelectedValue, IconSize)
                If IconErzeugt = True Then
                    rnfIconErzeugt.Visible = True
                    Page.ClientScript.RegisterClientScriptBlock([GetType](), "CloseScript", "redirectParentPage('IconVerwaltung.aspx')", True)
                Else
                    rnfIconNichtErzeugt.Visible = True
                End If
            Next
        Catch ex As Exception
            rnfIconNichtErzeugt.Visible = True
        End Try
    End If
End Sub

If i try to use InputStream i get a filenotfoundexeption. I added a Screanshot of this error.

So, what i'm doing wrong?

Thank you for reading.

9 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 31 Oct 2013, 09:59 AM
Hi Daniel,

When using TargetFolder property of RadAsyncUpload the uploaded file will get loaded to the TargetFolder on postback. The InputStream property is used to access the content of the uploaded files before saving them to a final target. In order to work with InputStream please try removing the TargerFolder from the code.

Thanks,
Shinu.
0
Daniel
Top achievements
Rank 1
answered on 31 Oct 2013, 10:07 AM
Yes if i remove TargetFolder it works. But the Problem is i need TargetFolder.
Or how will the control know where to save the images?
0
Shinu
Top achievements
Rank 2
answered on 01 Nov 2013, 05:04 AM
Hi Daniel,

Please try adding the code inside RadAsyncUpload's FileUploaded event to work with TargetFolder and InputStream property. Here is the Sample code snippet I tried to  to save the uploaded file in DataBase using Inputstream.

ASPX:
<telerik:RadAsyncUpload ID="rauIconUpload" runat="server" Skin="MetroTouch" MaxFileInputsCount="1"
    TargetFolder="Uploads" OnFileUploaded="rauIconUpload_FileUploaded">
</telerik:RadAsyncUpload>
<telerik:RadButton ID="rbtnIconUpload" runat="server" Text="Speichern" Skin="MetroTouch">
</telerik:RadButton>

C#:
protected void rauIconUpload_FileUploaded(object sender, FileUploadedEventArgs e)
{
    Stream file = e.File.InputStream;
    byte[] data = new byte[file.Length];
    Response.Write(file.Length);
    file.Read(data, 0, Convert.ToInt32(file.Length));
    String connectionstring = WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
    SqlConnection sqlconn = new SqlConnection(connectionstring);
    try
    {
        sqlconn.Open();
        SqlCommand command = new SqlCommand("Insert into Pictable (msgid, pic1) Values (1, @pic)", sqlconn);
        command.Parameters.Add("@pic", SqlDbType.VarBinary).Value = data;
        command.ExecuteNonQuery();
    }
    finally
    {
        file.Close();
        sqlconn.Close();
    }
}

Hope this will helps you.
Thanks,
Shinu.
0
Daniel
Top achievements
Rank 1
answered on 01 Nov 2013, 07:45 AM
Thank you Shinu,

this works better. But now i get this Error:

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

The code works now, but at the End i get this Error.

This is my Code:
Protected Sub rauIconUpload_FileUploaded(sender As Object, e As FileUploadedEventArgs)
 
    If rtxtIconBezeichnung.Text = String.Empty Or rtxtIconBezeichnung.Text = Nothing Or CHKValidation(rtxtIconBezeichnung.Text) = False Then
        rnfUngueltigeEingabe.Visible = True
    Else
        Try
 
            Dim file As Stream = e.File.InputStream
            Dim img As System.Drawing.Image = System.Drawing.Image.FromStream(file)
 
            Dim h As Integer = img.Height
            Dim w As Integer = img.Width
            img.Dispose()
 
            Dim fileName As String = e.File.GetName()
            IconPfad = "~/img/icons/" & fileName
 
            If w = 16 And h = 16 Then
                IconSize = "16x16"
            ElseIf w = 32 And h = 32 Then
                IconSize = "32x32"
            Else
                rnfIconNichtErzeugt.Visible = True
            End If
 
            IconErzeugt = Datenzugriff.CRTNeuesIcon(rtxtIconBezeichnung.Text, IconPfad, rcbIconGruppe.SelectedValue, IconSize)
            If IconErzeugt = True Then
                rnfIconErzeugt.Visible = True
                Page.ClientScript.RegisterClientScriptBlock([GetType](), "CloseScript", "redirectParentPage('IconVerwaltung.aspx')", True)
            Else
                rnfIconNichtErzeugt.Visible = True
            End If
 
        Catch ex As Exception
            rnfIconNichtErzeugt.Visible = True
        End Try
    End If
 
End Sub

Whats wrong? :/
Thank you.

Daniel
0
Daniel
Top achievements
Rank 1
answered on 03 Nov 2013, 11:23 AM
any ideas?

Daniel
0
Accepted
Shinu
Top achievements
Rank 2
answered on 04 Nov 2013, 03:27 AM
Hi Daniel,

Please try to use the Using keyword, whenever streams are used  it is a very good idea to use using since it will release the stream for you. Please try the following code snippet which works fine at my end.

C#:
protected void rauIconUpload_FileUploaded(object sender, FileUploadedEventArgs e)
{
    string IconPfad, IconSize;
    Stream file = e.File.InputStream;
    if (rtxtIconBezeichnung.Text == string.Empty | rtxtIconBezeichnung.Text == null | CHKValidation(rtxtIconBezeichnung.Text) == false)
    {
        rnfUngueltigeEingabe.Visible = true;
    }
    else
    {
        try
        {
            using (Stream fileStream = e.File.InputStream)
            {
                using (System.Drawing.Image img = System.Drawing.Image.FromStream(fileStream))
                {
                    int h = img.Height;
                    int w = img.Width;
                    img.Dispose();
                    string fileName = e.File.GetName();
                    IconPfad = "~/img/icons/" + fileName;
                    if (w == 16 & h == 16)
                    {
                        IconSize = "16x16";
                    }
                    else if (w == 32 & h == 32)
                    {
                        IconSize = "32x32";
                    }
                    else
                    {
                        rnfIconNichtErzeugt.Visible = true;
                    }
                    IconErzeugt = Datenzugriff.CRTNeuesIcon(rtxtIconBezeichnung.Text, IconPfad, rcbIconGruppe.SelectedValue, IconSize);
                    if (IconErzeugt == true)
                    {
                        rnfIconErzeugt.Visible = true;
                        Page.ClientScript.RegisterClientScriptBlock(GetType(), "CloseScript", "redirectParentPage('IconVerwaltung.aspx')", true);
                    }
                    else
                    {
                        rnfIconNichtErzeugt.Visible = true;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            rnfIconNichtErzeugt.Visible = true;
        }
    }
}

Hope this will helps you.
Thanks,
Shinu.
0
Daniel
Top achievements
Rank 1
answered on 07 Nov 2013, 02:31 PM
Sorry for not answered. I finally tried your code but i have still the same Problem. What else can i do?
Should i contact the support?

Thank you.
Daniel.
0
Accepted
Shinu
Top achievements
Rank 2
answered on 08 Nov 2013, 03:43 AM
Hi Daniel,

Please remove the following line of code,Stream file = e.File.InputStream; from the above code I posted.

Thanks,
Shinu.
0
Daniel
Top achievements
Rank 1
answered on 08 Nov 2013, 08:20 AM
Thank you very much! You helped me immensely.

Daniel
Tags
AsyncUpload
Asked by
Daniel
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Daniel
Top achievements
Rank 1
Share this question
or