Need help with filenotfoundexception

10 posts, 2 answers
  1. Daniel
    Daniel avatar
    35 posts
    Member since:
    Jan 2011

    Posted 30 Oct 2013 Link to this post

    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.
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 31 Oct 2013 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Daniel
    Daniel avatar
    35 posts
    Member since:
    Jan 2011

    Posted 31 Oct 2013 Link to this post

    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?
  5. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 01 Nov 2013 Link to this post

    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.
  6. Daniel
    Daniel avatar
    35 posts
    Member since:
    Jan 2011

    Posted 01 Nov 2013 Link to this post

    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
  7. Daniel
    Daniel avatar
    35 posts
    Member since:
    Jan 2011

    Posted 03 Nov 2013 Link to this post

    any ideas?

    Daniel
  8. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 03 Nov 2013 Link to this post

    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.
  9. Daniel
    Daniel avatar
    35 posts
    Member since:
    Jan 2011

    Posted 07 Nov 2013 Link to this post

    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.
  10. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 07 Nov 2013 Link to this post

    Hi Daniel,

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

    Thanks,
    Shinu.
  11. Daniel
    Daniel avatar
    35 posts
    Member since:
    Jan 2011

    Posted 08 Nov 2013 Link to this post

    Thank you very much! You helped me immensely.

    Daniel
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017