asp.net中的上传图片问题以及create错误 [英] problem with upload image in asp.net and error with create
问题描述
大家好..
我正在与vb.net一起在asp.net中工作,以便在站点中创建组
但是我遇到了与此代码有关的问题,但是我认为我的代码是正确的.问题是,当我运行代码时,图片没有上传,并返回一个错误.请帮帮我.
在group.aspx中,我这样写:
Hi all..
I am working in asp.net with vb.net to create a group in the site
but I faced a problem with this code, but I feel that my code is correct. The problem is, when I run the code, the picture does not upload, and return there was an error. Please help me.
in group.aspx i wrote this:
<asp:FileUpload ID="imgUpload" runat="server" />
<br />
<asp:Label ID="lblResult" runat="server" ForeColor="#0066FF"></asp:Label>
<br />
<asp:Image ID="Image1" style="width:200px" Runat="server" />
并以代码方式
and in code-behand
Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Configuration
Imports System.Data.OleDb
Imports System
Imports System.Configuration
Imports System.Web
Imports System.IO
Partial Class creatgroup
Inherits System.Web.UI.Page
Private Shared connectionString As String = _
WebConfigurationManager.ConnectionStrings("Pubs").ConnectionString
Private myConnection As New SqlConnection(connectionString)
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim img As FileUpload = CType(imgUpload, FileUpload)
Dim imgByte As Byte() = Nothing
If img.HasFile AndAlso Not img.PostedFile Is Nothing Then
''To create a PostedFile
Dim File As HttpPostedFile = imgUpload.PostedFile
''Create byte Array with file len
imgByte = New Byte(File.ContentLength - 1) {}
''force the control to load data in array
File.InputStream.Read(imgByte, 0, File.ContentLength)
End If
'' Insert the Group info and image into db
myConnection.Open()
Dim sql As String = "INSERT INTO Group(GroupName,Pic,Description,category,Privacy,News) VALUES(@enm, @eimg, @Description, @category, @Privacy, @News) @@IDENTITY"
Dim cmd As SqlCommand = New SqlCommand(sql, myConnection)
cmd.Parameters.AddWithValue("@enm", GroupName.Text)
cmd.Parameters.AddWithValue("@eimg", imgByte)
cmd.Parameters.AddWithValue("@Description", Description.Text)
cmd.Parameters.AddWithValue("@category", Category.SelectedValue)
If Privacy.Checked Then
cmd.Parameters.AddWithValue("@Privacy", "True")
Else
cmd.Parameters.AddWithValue("@Privacy", "False")
End If
cmd.Parameters.AddWithValue("@News", News.Text)
Dim id As String = cmd.ExecuteScalar.ToString
lblResult.Text = String.Format("Group ID is {0}", id)
Catch
lblResult.Text = "There was an error"
Finally
myConnection.Close()
End Try
End Sub
End Class
问候:)
Regards :)
推荐答案
为什么在此行的末尾有大括号:
Why do you have curly braces at the end of this line :
imgByte = New Byte(File.ContentLength - 1) {}
Dim img As FileUpload = CType(imgUpload,FileUpload)
Dim img As FileUpload = CType(imgUpload, FileUpload)
为什么需要强制转换?为什么它不是正确的类型?我认为这是页面上的控件吗?
您是否已遍历此代码以查看会发生什么?是imgByte Nothing打数据库代码吗?为什么即使没有图像也要运行数据库代码?
Why do you need to cast this ? Why is it not the right type already ? I assume this is the control on the page?
Have you stepped through this code to see what happens ? Is imgByte Nothing when it hits the DB code ? Why do you have the DB code run even if there is no image ?
这篇关于asp.net中的上传图片问题以及create错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!