Toon posts:

ASP.net uploaden file probleem..

Pagina: 1
Acties:
  • 55 views sinds 30-01-2008

Verwijderd

Topicstarter
Voor een admin deel van een webshop is er een vak voor het uploaden van een afbeelding.

Deze afbeelding moet eerst in een temp map worden opgeslagen.

Echter gaat het hier fout.. de afbeelding wil op een of het andere manier niet worden opgeslagen... wat ik ook doe

Hieronder zie je de .vb code die wordt gebruikt, alles gaat goed als ik geen foto invoer.. en de andere gegevens wel. Dan wordt het keurig opgeslagen.
Maar met een foto erbij dan gaat hij de foto uploaden en poef wordt het scherm compleet wit.. en er gebeurt niets..

Hoe kan dit of wat doe ik fout met het uploaden van de foto..

De code van het deel wat mis gaat:

code:
1
2
3
4
5
6
7
8
9
10
11
                        '===============================================================================
                        '== SET THE PATH TO YOUR PRODUCT IMAGES, THUMBS AND TEMP FOLDERS IN WEB.CONFIG
                        '===============================================================================
                        Dim strPath As String = strTempPath & strFileName
                        Dim strPath2 As String = strImagePath & strFileName
                        Dim strPathThumb As String = strThumbPath & strFileName
                        '===============================================================================

                        File.SaveAs(strPath)

                        qual.GetImageToResize(strFileName, strPath, strPath2, strPathThumb, strContType)


Het gaat dus fout op File.SaveAs(strPath); de functie daaronder gaat ook niet goed.. dat komt omdat die de foto wil gebruiken die net is opgeslagen wat dus niet goed gaat dus dat is logisch.. daar zit de fout niet in. het gaat 100% zeker fout op File.SaveAs

Of zoek ik het probleem verkeerd en ligt het aan de gebruikte path's ? in de web.config file... zie hieronder...

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  <appSettings>
    <!-- Set this key to reflect your dB connection (this one is for MS SQL Server) -->
    <add key="ConnectionString" value="server=localhost;Trusted_Connection=true;database=Store"  />
    <!-- Set this key to reflect the physical path to your store's product images (for saving scaled down product images) -->
    <add key="ImageDir" value="C:\StoreVBVS\StoreVBVS\ProductImages\"  />
    <!-- Set this key to reflect the physical path to your store's product thumbnails (for saving scaled down product images) -->
    <add key="ThumbDir" value="C:\StoreVBVS\StoreVBVS\ProductImages\thumbs\"  />
    <!-- Set this key to reflect the physical path to your store's temporary folder (used when resizing images) -->
    <add key="TempDir" value="C:\StoreVBVS\StoreVBVS\ProductImages\temp\"  />
    <!-- Set this key to reflect the web or virtual path to your store's thumbnail images (for thumbs shown in the products list) -->
    <add key="RemoteImagePath" value="http://localhost/storevbvs/ProductImages/thumbs/"  />
    <!-- Set this key to reflect the web or virtual path to your store's thumbnail images (for thumbs shown in the products list) -->
    <add key="LocalImagePath" value="http://localhost/storevbvs/ProductImages/thumbs/"  />
    <!-- Set this key to reflect an e-mail address to identify which site has had an error (global.asax.vb) -->
    <add key="SiteMailAddress" value="YourApp@YourDomain.com"  />
    <!-- Set this key to reflect an e-mail address to receive error messages (global.asax.vb) -->
    <add key="AdminMailAddress" value="AppAdmin@YourDomain.com"  />
    <!-- Set this key to reflect your your smtp server (Defaults to 127.0.0.1) -->
    <add key="smtpserver" value="127.0.0.1"  />


Complete code:

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
****************************************************************************************************

Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO
Imports Microsoft.VisualBasic


Public Class NyProd

    Inherits Page

    Protected WithEvents lblOk As Label
    Protected WithEvents lblCurr As Label
    Protected WithEvents drpKat As DropDownList
    Protected WithEvents txtProd As TextBox
    Protected WithEvents txtArtnr As TextBox
    Protected WithEvents txtPris As TextBox
    Protected WithEvents txtDesc As TextBox
    Protected WithEvents btnUpload As Button
    Protected WithEvents RequiredFieldValidator1 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents RequiredFieldValidator2 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents RequiredFieldValidator3 As System.Web.UI.WebControls.RequiredFieldValidator
    Protected WithEvents RegularExpressionValidator1 As System.Web.UI.WebControls.RegularExpressionValidator

    Public strConn As String = ConfigurationSettings.Appsettings("ConnectionString")
    Public strImagePath As String = ConfigurationSettings.AppSettings("ImageDir")
    Public strThumbPath As String = ConfigurationSettings.AppSettings("ThumbDir")
    Public strTempPath As String = ConfigurationSettings.AppSettings("TempDir")


    Sub Page_Load(ByVal s As Object, ByVal e As EventArgs) Handles MyBase.Load

        lblCurr.Text = ConfigurationSettings.AppSettings("Currency")

        If Not IsPostBack Then

            If Not System.IO.Directory.Exists(strTempPath) Then
                System.IO.Directory.CreateDirectory(strTempPath)
            End If

            Try
                Dim conFamily As SqlConnection = New SqlConnection(strConn)
                Dim myDataReader As SqlDataReader
                Dim cmdSelect As New SqlCommand("StoreAdmin_SelectCategoryProducts", conFamily)
                cmdSelect.CommandType = CommandType.StoredProcedure

                Dim strIns As String = "-- Choose product category --"
                conFamily.Open()
                myDataReader = cmdSelect.ExecuteReader()

                drpKat.DataSource = myDataReader
                drpKat.DataBind()
                drpKat.Items.Insert(0, New ListItem(strIns, "0"))
                drpKat.SelectedIndex = 0

                myDataReader.Close()
                conFamily.Close()
            Catch
                Response.Redirect("ErrorPage.aspx?referrer=nyprod.aspx&Function=Page_Load")
            End Try

        End If
    End Sub

    Sub Button_Click(ByVal s As Object, ByVal e As EventArgs) Handles btnUpload.Click
        If drpKat.SelectedItem.Value = "0" Then
            lblOk.Text = "<font color='#FF0000'><strong>** You must choose a product category **</strong></font><br><br>"
            Response.Clear()
        Else
            Dim File As HttpPostedFile
            File = Request.Files.Get(0)
            If File.ContentLength <= 0 Then
                lblOk.Text = "<font color='#FF0000'><strong>** You did not include an image **</strong></font><br><br>"
                Exit Sub
            Else
                Dim qual As New SetJpegQuality()
                Dim strFileName As String = qual.ExtractFileName(File.FileName)
                If File.ContentLength > (1024 * Convert.ToInt32(ConfigurationSettings.AppSettings("imgmaxsize"))) Then
                    lblOk.Text = "<font color='#FF0000'><strong>** File is too large (max size " & ConfigurationSettings.AppSettings("imgmaxsize") & "kb) **</strong></font><br><br>"
                    Exit Sub
                Else
                    If Not (File.ContentType = ("image/pjpeg").ToLower OrElse File.ContentType = ("image/gif").ToLower OrElse File.ContentType = ("image/jpeg").ToLower) Then
                        lblOk.Text = "<font color='#FF0000'><strong>** Wrong format. Image must be JPEG or GIF format. **</strong></font><br><br>"
                        Exit Sub
                    Else

                        Dim strContType As String = File.ContentType.ToString.ToLower

                        '===============================================================================
                        '== SET THE PATH TO YOUR PRODUCT IMAGES, THUMBS AND TEMP FOLDERS IN WEB.CONFIG
                        '===============================================================================
                        Dim strPath As String = strTempPath & strFileName
                        Dim strPath2 As String = strImagePath & strFileName
                        Dim strPathThumb As String = strThumbPath & strFileName
                        '===============================================================================

                        File.SaveAs(strPath)

                        qual.GetImageToResize(strFileName, strPath, strPath2, strPathThumb, strContType)

                        Try
                            Dim intcatid As Integer = drpKat.SelectedItem.Value
                            Dim strmodno As String = Trim(Replace(txtArtnr.Text, "'", "''"))
                            Dim strmodname As String = Trim(Replace(txtProd.Text, "'", "''"))
                            Dim decprice As Decimal = Trim(Replace(txtPris.Text, "'", "''"))
                            Dim strdesc As String = Trim(Replace(txtDesc.Text, "'", "''"))

                            Dim confamily As SqlConnection = New SqlConnection(strConn)
                            Dim cmdselect As New SqlCommand("storeadmin_insertproductandimage", confamily)
                            cmdselect.CommandType = CommandType.StoredProcedure

                            cmdselect.Parameters.Add("@intcatid", SqlDbType.Int).Value = intcatid
                            cmdselect.Parameters.Add("@strmodno", strmodno)
                            cmdselect.Parameters.Add("@strmodname", strmodname)
                            cmdselect.Parameters.Add("@strfilename", strFileName)
                            cmdselect.Parameters.Add("@decprice", SqlDbType.Money).Value = decprice
                            cmdselect.Parameters.Add("@strdesc", strdesc)

                            confamily.Open()
                            cmdselect.ExecuteNonQuery()
                            confamily.Close()
                        Catch
                            Response.Redirect("errorpage.aspx?referrer=nyprod.aspx&function=button_click")
                        End Try
                        lblOk.Font.Bold = True
                        lblOk.ForeColor = Color.FromName("#008000")
                        lblOk.Text = "** image and data has been saved **<br><br>"
                    End If
                End If
            End If
        End If
    End Sub

    Private Sub InitializeComponent()

    End Sub

End Class


Please help.. ben er nl. al een aantal dagen mee bezig en het wil niet lukken...
Heeft iemand misschien een werkend simpel voorbeeld voor het gewoon opslaan van een foto ? of bestand...

Alvast bedankt

  • gorgi_19
  • Registratie: Mei 2002
  • Laatst online: 20-08 11:40

gorgi_19

Kruimeltjes zijn weer op :9

Zet eens een aantal breakpoints neer en gooi eens exceptions.

Dit is vooralsnog debugwerk, waarbij je de errors netjes hebt gemaskeerd.

Digitaal onderwijsmateriaal, leermateriaal voor hbo


Verwijderd

De code ziet er redelijk copy paste uit dus zal het waarschijnlijk iets met rechten te maken hebben. Kan de asp.net user schrijven in die map? Maar dit is gissen zonder foutmelding te zien.

Verwijderd

Topicstarter
get ging precies fout bij file.saveas .... dus ik zocht daar de fout ook.. na een aantal rechten etc. van de betreffende mappen hier te veranderen en dan de code weer uploaden werkte het dus wel weer.. vreemd..

  • whoami
  • Registratie: December 2000
  • Laatst online: 11:10
Gewoon een kwestie van debuggen.

https://fgheysels.github.io/


Dit topic is gesloten.