使用现有的aspx / vb.net网站上传集成probs [英] Uploadify integration probs with existing aspx/vb.net website
问题描述
您好b $ b
我正在尝试将Uploadify上传进度插件与现有的将图片上传到数据库的asp / vb.net网站一起使用。
现有站点使用asp.fileupload组件,上传操作是通过单独的上传按钮。所有代码都在asp表单中的aspx.vb文件。
好的,我已经把gui方面排序了但是我不确定如何让uploadify触发现有的btnUpload_Click事件。
大概是问题在于引导'uploader'变量。我看到很多人使用泛型处理程序ashx例程但我不知道如何将我的btnUpload代码移动到此并保留所有表单输入信息。
有人可以告诉我如何处理这个问题吗?
我在HEAD部分的页面中有这个代码:
Hi
I'm trying to get the 'Uploadify' upload progress plugin working with an existing asp/vb.net site that uploads pictures to a database.
The existing site uses an asp.fileupload component and the upload action is via a separate upload button. All code for this is in the asp forms aspx.vb file.
Ok, I've got the gui side of things sorted but I'm not sure how to get uploadify to fire the existing btnUpload_Click event.
Presumably teh problem is with directing the 'uploader' variable. I see a lot of people utilising generic handler ashx routines but I have no idea how to move my btnUpload code to this and retain all the form input info.
Can someone advise me on how to deal with this problem?
I have this code in the pages HEAD section:
<link href="~/Styles/uploadify.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="Uploadify/jquery.uploadify.min.js"></script>
<script type="text/javascript">
$(function() {
$("#<%=fupImage.ClientID %>").uploadify({
'swf' : 'Uploadify/uploadify.swf',
'uploader' : 'uploadify.php',
'buttonText': 'Select Image',
'cancelImg' : 'images/uploadify-cancel.png',
'folder' : 'uploads',
'fileDesc' : 'Image Files',
'fileExt' : 'Image Files (.JPG, .JPEG, .GIF, .BMP, .PNG)',
'sizeLimit' : 4000000,
'auto' : false,
'multi' : false
});
});
</script>
这里是我的btnUpload代码:
and here's my btnUpload code:
Protected Sub btnUpload_Click(sender As Object, e As EventArgs) Handles btnUpload.Click
pnlConfirmation.Visible = False
lblUploadResult.Text = ""
lblUploadResult.ForeColor = Color.DarkGreen
'validate all fields before uploading
If Not validateControls() Then
lblUploadResult.Text += vbCrLf + "Try again."
lblUploadResult.ForeColor = Color.Red
pnlConfirmation.Visible = True
Exit Sub
End If
'check for any other entries by this person
If checkOtherEntries() Then
lblUploadResult.Text = "There is already an entry for this email address." + vbCrLf + _
"If you feel this is an error then please email" + vbCrLf + _
appConfiguration.ErrorLogEmail
lblUploadResult.ForeColor = Color.Red
pnlConfirmation.Visible = True
Exit Sub
End If
'**All validated so allow the upload
'check to make sure a file is selected before adding it to the db
If fupImage.PostedFile IsNot Nothing AndAlso fupImage.HasFile AndAlso Not String.IsNullOrEmpty(fupImage.PostedFile.FileName) AndAlso CustomValidatorFormat.IsValid Then
'check for Tsan connection. If none then save locally
Dim tsanFolder As String = ConfigurationManager.AppSettings("TsanLocalFolder")
If (Not Directory.Exists(tsanFolder)) Then
Directory.CreateDirectory(tsanFolder)
End If
Dim thumbsFolder As String = ConfigurationManager.AppSettings("MailThumbsFolder")
If (Not Directory.Exists(thumbsFolder)) Then
Directory.CreateDirectory(thumbsFolder)
End If
'store the currently selected file in memeory
Dim img As HttpPostedFile = fupImage.PostedFile
'Make sure a duplicate file doesn’t exist on Tsan. If it does, keep on appending an incremental numeric until filename is unique
Dim sFilename As String = System.IO.Path.GetFileName(img.FileName)
'check if filename exists on Tsan - increment name if it does
Dim newFileName As String = getTSANfilename(tsanFolder, sFilename)
'save to TsanFolder- full res
' fupImage.PostedFile.SaveAs(sFolder & sFilename)
img.SaveAs(tsanFolder & newFileName)
'make a thumb and save to dB
Dim imageToBeResized As System.Drawing.Image = System.Drawing.Image.FromStream(fupImage.PostedFile.InputStream)
Dim imageHeight As Integer = imageToBeResized.Height
Dim imageWidth As Integer = imageToBeResized.Width
Dim maxWidth As Integer = 300
Dim iDivisor As Integer = imageWidth / maxWidth
imageHeight = imageHeight / iDivisor
imageWidth = maxWidth
Dim bitmap As New Bitmap(imageToBeResized, imageWidth, imageHeight)
Dim stream As System.IO.MemoryStream = New MemoryStream()
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg)
stream.Position = 0
Dim image As Byte() = New Byte(Stream.Length) {}
stream.Read(image, 0, image.Length)
'connect to the db
Dim conn As New SqlConnection(WebConfigurationManager.ConnectionStrings("dBconnString").ConnectionString)
'sql command to save our image data to the db
Dim cmd As New SqlCommand("INSERT INTO EntryData(imgBin, imgFileNameSubmitted, imgFileNameSaved, imgSize, imgType, imgDate, imgDescription, NameFirst, NameLast, Email, Phone, Address_Unit, Address_Number, Address_Street1, Address_Street2, Address_Suburb, Address_State, Address_PostCode, Address_Country, Referral, imgTitle) " & _
"VALUES (@imgBin, @imgFileNameSubmitted, @imgFileNameSaved, @imgSize, @imgType, @imgDate, @imgDescription, @NameFirst, @NameLast, @Email, @Phone, @Address_Unit, @Address_Number, @Address_Street1, @Address_Street2, @Address_Suburb, @Address_State, @Address_PostCode, @Address_Country, @Referral, @imgTitle) " & _
"SELECT @@IDENTITY", conn)
cmd.CommandType = CommandType.Text
'add thumbnail img binary data
' cmd.Parameters.Add("@imgBin", SqlDbType.Image, imgBin.Length).Value = imgBin
cmd.Parameters.Add("@imgBin", SqlDbType.Image, image.Length).Value = image
'add submitted img file name
cmd.Parameters.AddWithValue("@imgFileNameSubmitted", sFilename)
'add saved img file name
cmd.Parameters.AddWithValue("@imgFileNameSaved", newFileName)
'add img size
cmd.Parameters.AddWithValue("@imgSize", img.ContentLength) 'imgBin.Length)
'add img type
cmd.Parameters.AddWithValue("@imgType", img.ContentType)
'add img date
cmd.Parameters.AddWithValue("@imgDate", DateTime.Parse(Now.Date()))
'add img message
cmd.Parameters.AddWithValue("@imgDescription", txtDescription.Text)
'add img title
cmd.Parameters.AddWithValue("@imgTitle", txtTitle.Text)
'add first name
cmd.Parameters.AddWithValue("@NameFirst", txtFirstName.Text)
'add last name
cmd.Parameters.AddWithValue("@NameLast", txtLastName.Text)
'add email
cmd.Parameters.AddWithValue("@Email", txtEmail.Text)
'add phone number
cmd.Parameters.AddWithValue("@Phone", txtPhone.Text)
'add unit number
cmd.Parameters.AddWithValue("@Address_Unit", txtUnit.Text)
'add street number
cmd.Parameters.AddWithValue("@Address_Number", txtNumber.Text)
'add street 1
cmd.Parameters.AddWithValue("@Address_Street1", txtStreet.Text)
'add street 2
cmd.Parameters.AddWithValue("@Address_Street2", txtStreet2.Text)
'add Suburb
cmd.Parameters.AddWithValue("@Address_Suburb", txtSuburb.Text)
'add state
cmd.Parameters.AddWithValue("@Address_State", ddlState.Text)
'add post code
cmd.Parameters.AddWithValue("@Address_PostCode", txtPostCode.Text)
'add country
cmd.Parameters.AddWithValue("@Address_Country", "Australia")
'add referral info
If RadioButton1.Checked Then
cmd.Parameters.AddWithValue("@Referral", RadioButton1.Text)
ElseIf RadioButton2.Checked Then
cmd.Parameters.AddWithValue("@Referral", RadioButton2.Text)
ElseIf RadioButton3.Checked Then
cmd.Parameters.AddWithValue("@Referral", RadioButton3.Text)
ElseIf RadioButton4.Checked Then
cmd.Parameters.AddWithValue("@Referral", RadioButton4.Text)
ElseIf RadioButton5.Checked Then
cmd.Parameters.AddWithValue("@Referral", RadioButton5.Text)
ElseIf RadioButton6.Checked Then
cmd.Parameters.AddWithValue("@Referral", RadioButton6.Text)
ElseIf RadioButton7.Checked Then
cmd.Parameters.AddWithValue("@Referral", RadioButton7.Text)
ElseIf RadioButton8.Checked Then
cmd.Parameters.AddWithValue("@Referral", RadioButton8.Text)
End If
Dim id As Integer
Using conn
'open the connection
conn.Open()
'send the sql query to store the data
Try
id = Convert.ToInt32(cmd.ExecuteScalar())
' lblUploadResult.Text = String.Format("Picture ID is {0}", id)
lblUploadResult.Text = "Photo submitted.<br />Thank you for your entry"
' Display the image from the tsan
Image1.ImageUrl = "~/ShowImage.ashx?id=" & id
Image1.Visible = True
Catch ex As Exception
Dim str As String = ex.Message
lblUploadResult.ForeColor = Color.Red
lblUploadResult.Text = "There was an upload error.<br />" + str
End Try
cmd.Dispose()
End Using
'Display the result of the upload.
pnlConfirmation.Visible = True
'send some notification emails
Dim sArgs As String = id.ToString + "," + sFilename + "," + newFileName + "," + txtFirstName.Text + "," + txtLastName.Text + "," + txtUnit.Text + "," + txtNumber.Text + "," + _
txtStreet.Text + "," + txtStreet2.Text + "," + txtSuburb.Text + "," + ddlState.Text + "," + txtPostCode.Text + "," + "Australia" + "," + txtPhone.Text + "," + _
txtEmail.Text + "," + txtTitle.Text + "," + tsanFolder + "," + thumbsFolder
Dim w As New Work()
w.Data = sArgs
Dim newThread As New Thread(AddressOf Work.DoWork)
newThread = New Thread(AddressOf w.DoMoreWork)
newThread.Start()
End If
End Sub
推荐答案
( function () {
( #<%= fupImage.ClientID%> )。uploadify({
' swf':' Uploadify / uploadify.swf',
' uploader':' uploadify.php',
' buttonText':' 选择图像',
' cancelImg':' images / uploadify-cancels.png',
' folder' :' uploads',
' fileDesc':' 图像文件',
' fileExt':' 图像文件(.JPG,.JPEG,.GIF,.BMP,.PNG)',
' sizeLimit': 4000000 ,
' auto': false ,
' multi': false
});
});
< / script>
("#<%=fupImage.ClientID %>").uploadify({ 'swf' : 'Uploadify/uploadify.swf', 'uploader' : 'uploadify.php', 'buttonText': 'Select Image', 'cancelImg' : 'images/uploadify-cancel.png', 'folder' : 'uploads', 'fileDesc' : 'Image Files', 'fileExt' : 'Image Files (.JPG, .JPEG, .GIF, .BMP, .PNG)', 'sizeLimit' : 4000000, 'auto' : false, 'multi' : false }); }); </script>
这里是我的btnUpload代码:
and here's my btnUpload code:
Protected Sub btnUpload_Click(sender As Object, e As EventArgs) Handles btnUpload.Click
pnlConfirmation.Visible = False
lblUploadResult.Text = ""
lblUploadResult.ForeColor = Color.DarkGreen
'validate all fields before uploading
If Not validateControls() Then
lblUploadResult.Text += vbCrLf + "Try again."
lblUploadResult.ForeColor = Color.Red
pnlConfirmation.Visible = True
Exit Sub
End If
'check for any other entries by this person
If checkOtherEntries() Then
lblUploadResult.Text = "There is already an entry for this email address." + vbCrLf + _
"If you feel this is an error then please email" + vbCrLf + _
appConfiguration.ErrorLogEmail
lblUploadResult.ForeColor = Color.Red
pnlConfirmation.Visible = True
Exit Sub
End If
'**All validated so allow the upload
'check to make sure a file is selected before adding it to the db
If fupImage.PostedFile IsNot Nothing AndAlso fupImage.HasFile AndAlso Not String.IsNullOrEmpty(fupImage.PostedFile.FileName) AndAlso CustomValidatorFormat.IsValid Then
'check for Tsan connection. If none then save locally
Dim tsanFolder As String = ConfigurationManager.AppSettings("TsanLocalFolder")
If (Not Directory.Exists(tsanFolder)) Then
Directory.CreateDirectory(tsanFolder)
End If
Dim thumbsFolder As String = ConfigurationManager.AppSettings("MailThumbsFolder")
If (Not Directory.Exists(thumbsFolder)) Then
Directory.CreateDirectory(thumbsFolder)
End If
'store the currently selected file in memeory
Dim img As HttpPostedFile = fupImage.PostedFile
'Make sure a duplicate file doesn’t exist on Tsan. If it does, keep on appending an incremental numeric until filename is unique
Dim sFilename As String = System.IO.Path.GetFileName(img.FileName)
'check if filename exists on Tsan - increment name if it does
Dim newFileName As String = getTSANfilename(tsanFolder, sFilename)
'save to TsanFolder- full res
' fupImage.PostedFile.SaveAs(sFolder & sFilename)
img.SaveAs(tsanFolder & newFileName)
'make a thumb and save to dB
Dim imageToBeResized As System.Drawing.Image = System.Drawing.Image.FromStream(fupImage.PostedFile.InputStream)
Dim imageHeight As Integer = imageToBeResized.Height
Dim imageWidth As Integer = imageToBeResized.Width
Dim maxWidth As Integer = 300
Dim iDivisor As Integer = imageWidth / maxWidth
imageHeight = imageHeight / iDivisor
imageWidth = maxWidth
Dim bitmap As New Bitmap(imageToBeResized, imageWidth, imageHeight)
Dim stream As System.IO.MemoryStream = New MemoryStream()
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg)
stream.Position = 0
Dim image As Byte() = New Byte(Stream.Length) {}
stream.Read(image, 0, image.Length)
'connect to the db
Dim conn As New SqlConnection(WebConfigurationManager.ConnectionStrings("dBconnString").ConnectionString)
'sql command to save our image data to the db
Dim cmd As New SqlCommand("INSERT INTO EntryData(imgBin, imgFileNameSubmitted, imgFileNameSaved, imgSize, imgType, imgDate, imgDescription, NameFirst, NameLast, Email, Phone, Address_Unit, Address_Number, Address_Street1, Address_Street2, Address_Suburb, Address_State, Address_PostCode, Address_Country, Referral, imgTitle) " & _
"VALUES (@imgBin, @imgFileNameSubmitted, @imgFileNameSaved, @imgSize, @imgType, @imgDate, @imgDescription, @NameFirst, @NameLast, @Email, @Phone, @Address_Unit, @Address_Number, @Address_Street1, @Address_Street2, @Address_Suburb, @Address_State, @Address_PostCode, @Address_Country, @Referral, @imgTitle) " & _
"SELECT @@IDENTITY", conn)
cmd.CommandType = CommandType.Text
'add thumbnail img binary data
' cmd.Parameters.Add("@imgBin", SqlDbType.Image, imgBin.Length).Value = imgBin
cmd.Parameters.Add("@imgBin", SqlDbType.Image, image.Length).Value = image
'add submitted img file name
cmd.Parameters.AddWithValue("@imgFileNameSubmitted", sFilename)
'add saved img file name
cmd.Parameters.AddWithValue("@imgFileNameSaved", newFileName)
'add img size
cmd.Parameters.AddWithValue("@imgSize", img.ContentLength) 'imgBin.Length)
'add img type
cmd.Parameters.AddWithValue("@imgType", img.ContentType)
'add img date
cmd.Parameters.AddWithValue("@imgDate", DateTime.Parse(Now.Date()))
'add img message
cmd.Parameters.AddWithValue("@imgDescription", txtDescription.Text)
'add img title
cmd.Parameters.AddWithValue("@imgTitle", txtTitle.Text)
'add first name
cmd.Parameters.AddWithValue("@NameFirst", txtFirstName.Text)
'add last name
cmd.Parameters.AddWithValue("@NameLast", txtLastName.Text)
'add email
cmd.Parameters.AddWithValue("@Email", txtEmail.Text)
'add phone number
cmd.Parameters.AddWithValue("@Phone", txtPhone.Text)
'add unit number
cmd.Parameters.AddWithValue("@Address_Unit", txtUnit.Text)
'add street number
cmd.Parameters.AddWithValue("@Address_Number", txtNumber.Text)
'add street 1
cmd.Parameters.AddWithValue("@Address_Street1", txtStreet.Text)
'add street 2
cmd.Parameters.AddWithValue("@Address_Street2", txtStreet2.Text)
'add Suburb
cmd.Parameters.AddWithValue("@Address_Suburb", txtSuburb.Text)
'add state
cmd.Parameters.AddWithValue("@Address_State", ddlState.Text)
'add post code
cmd.Parameters.AddWithValue("@Address_PostCode", txtPostCode.Text)
'add country
cmd.Parameters.AddWithValue("@Address_Country", "Australia")
'add referral info
If RadioButton1.Checked Then
cmd.Parameters.AddWithValue("@Referral", RadioButton1.Text)
ElseIf RadioButton2.Checked Then
cmd.Parameters.AddWithValue("@Referral", RadioButton2.Text)
ElseIf RadioButton3.Checked Then
cmd.Parameters.AddWithValue("@Referral", RadioButton3.Text)
ElseIf RadioButton4.Checked Then
cmd.Parameters.AddWithValue("@Referral", RadioButton4.Text)
ElseIf RadioButton5.Checked Then
cmd.Parameters.AddWithValue("@Referral", RadioButton5.Text)
ElseIf RadioButton6.Checked Then
cmd.Parameters.AddWithValue("@Referral", RadioButton6.Text)
ElseIf RadioButton7.Checked Then
cmd.Parameters.AddWithValue("@Referral", RadioButton7.Text)
ElseIf RadioButton8.Checked Then
cmd.Parameters.AddWithValue("@Referral", RadioButton8.Text)
End If
Dim id As Integer
Using conn
'open the connection
conn.Open()
'send the sql query to store the data
Try
id = Convert.ToInt32(cmd.ExecuteScalar())
' lblUploadResult.Text = String.Format("Picture ID is {0}", id)
lblUploadResult.Text = "Photo submitted.<br />Thank you for your entry"
' Display the image from the tsan
Image1.ImageUrl = "~/ShowImage.ashx?id=" & id
Image1.Visible = True
Catch ex As Exception
Dim str As String = ex.Message
lblUploadResult.ForeColor = Color.Red
lblUploadResult.Text = "There was an upload error.<br />" + str
End Try
cmd.Dispose()
End Using
'Display the result of the upload.
pnlConfirmation.Visible = True
'send some notification emails
Dim sArgs As String = id.ToString + "," + sFilename + "," + newFileName + "," + txtFirstName.Text + "," + txtLastName.Text + "," + txtUnit.Text + "," + txtNumber.Text + "," + _
txtStreet.Text + "," + txtStreet2.Text + "," + txtSuburb.Text + "," + ddlState.Text + "," + txtPostCode.Text + "," + "Australia" + "," + txtPhone.Text + "," + _
txtEmail.Text + "," + txtTitle.Text + "," + tsanFolder + "," + thumbsFolder
Dim w As New Work()
w.Data = sArgs
Dim newThread As New Thread(AddressOf Work.DoWork)
newThread = New Thread(AddressOf w.DoMoreWork)
newThread.Start()
End If
End Sub
这篇关于使用现有的aspx / vb.net网站上传集成probs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!