Friday, March 9, 2012

File Upload Help

Hi, I am trying to upload a file to database.

I have used the following code, every loads good to the database apart from the image, does it go anywhere?

I have created a column in the table called 'FileUploadAdvert' and made it an image?

Any ideas where I'm going wrong?

Thanks

Gordon

ProtectedSub btnAdvertSubmit_Click(ByVal senderAsObject,ByVal eAs System.EventArgs)Handles btnAdvertSubmit.Click

Dim dashDataSourceAsNew SqlDataSource()dashDataSource.ConnectionString = ConfigurationManager.ConnectionStrings("DashConnectionString1").ToString()

dashDataSource.InsertCommandType = SqlDataSourceCommandType.Text

dashDataSource.InsertCommand ="INSERT INTO tblAdvert (AdvertOwner, AdvertName, TopLeftH, TopLeftV, Height, Width, ToolTip, WebLink, AcceptTerms, DateTimeStamp, IPAddress) VALUES (@.AdvertOwner, @.AdvertName, @.TopLeftH, @.TopLeftV, @.Height, @.Width, @.ToolTip, @.WebLink, @.AcceptTerms, @.DateTimeStamp, @.IPAddress)"

dashDataSource.InsertParameters.Add("AdvertOwner", txtName.Text)

dashDataSource.InsertParameters.Add("AdvertName", txtCompName.Text)

dashDataSource.InsertParameters.Add("TopLeftH", DropDownAccross.Text)

dashDataSource.InsertParameters.Add("TopLeftV", DropDownDown.Text)

dashDataSource.InsertParameters.Add("Height", DropDownHeight.Text)

dashDataSource.InsertParameters.Add("Width", DropDownWidth.Text)

dashDataSource.InsertParameters.Add("ToolTip", txtOver.Text)

dashDataSource.InsertParameters.Add("Weblink", txtURL.Text)

dashDataSource.InsertParameters.Add("AcceptTerms", CheckBoxAgree.Checked)

dashDataSource.InsertParameters.Add("IPAddress", Request.UserHostAddress.ToString)

dashDataSource.InsertParameters.Add("DateTimeStamp", DateTime.Now)

IfNot FileUploadAdvert.PostedFileIsNothingThen

Dim filepathAsString = FileUploadAdvert.PostedFile.FileName

Dim patAsString ="\\(?:.+)\\(.+)\.(.+)"

Dim rAs Regex =New Regex(pat)

'run

Dim mAs Match = r.Match(filepath)

Dim file_extAsString = m.Groups(2).Captures(0).ToString()

Dim filenameAsString = m.Groups(1).Captures(0).ToString()

Dim fileAsString = filename &"." & file_ext

'save the file to the server

FileUploadAdvert.PostedFile.SaveAs(Server.MapPath(".\") & file)

lblStatus.Text ="File Saved to: " & Server.MapPath(".\") & file

EndIf

Dim rowsAffectedAsInteger = 0

Try

rowsAffected = dashDataSource.Insert()

Catch exAs Exception

Server.Transfer("Register_Fail.aspx")

Finally

dashDataSource =Nothing

EndTry

If rowsAffected <> 1Then

Server.Transfer("Register_Fail.aspx")

Else

Server.Transfer("Register_Complete.aspx")

EndIf

EndSub

Looking at your code, all you have done is saved the uploaded file onto theweb server's file system.

You have not saved it into the database.

Ifyou want to save the actual image to your database you will need to geta byte array from the image, then update your insert statement toinclude the image and pass in the byte array as a binary type inputparameter.

Or the other way is just to store the file path of where you saved the image on your file system to your database.

|||

Any Idea how I do either of those? I've hit a bit of a dead end!

No comments:

Post a Comment