gianitoo
asked on
upload file to folder and insert to db
how do you upload a file into a folder called images and also save the file name into a column in sql server 2005. i have my code below. feel free to tweak it if needed
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:JMIConne ctionStrin g %>"
InsertCommand="INSERT INTO [Clientbrief] ([reserach]) VALUES (@reserach)"
<InsertParameters>
<asp:FormParameter FormField="Reserach" Name="Reserach" Type="String"
/>
Type="String" />
</InsertParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:JMIConne
InsertCommand="INSERT INTO [Clientbrief] ([reserach]) VALUES (@reserach)"
<InsertParameters>
<asp:FormParameter FormField="Reserach" Name="Reserach" Type="String"
/>
Type="String" />
</InsertParameters>
</asp:SqlDataSource>
ASKER
can u send it in vb.net
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
add a FileUpload control to your aspx page.
on Submit, get the file and read it as a ByteStream into a Bitmap or Image object.
Write that Bitmap to your Images folder, being sure to add the appropriate image flag (jpeg, bmp, gif, etc.). Finally send that file name to your sqldatasource.
The sample code is from one of my pages.
Example Code (in your aspx file):
<asp:FileUpload ID="photoFile1" runat="server" />
(in your code behind file):
HttpPostedFile uploadedImageFile = Request.Files[0];
if (uploadedImageFile.Content
{
if (System.IO.Path.GetExtensi
System.IO.Path.GetExtensio
{
lblPhotoErrors.Text = "One or more files that you selected are not JPEG or GIF format.";
return;
}
//**Get the image**
try
{
Bitmap uploadedPhoto = new Bitmap(uploadedImageFile.I
//save the image with a unique file name
string uploadFileName = System.IO.Path.GetFileName
int fileAppendNum = 0;
while (System.IO.File.Exists(Ser
{
fileAppendNum++;
uploadFileName = System.IO.Path.GetFileName
fileAppendNum.ToString() + ".jpg";
}
uploadedPhoto.Save(Server.
ImageFormat.Jpeg);
//TODO: Write uploadFileName to your sql database
//Destroy objects. This may not be necessary.
uploadedPhoto.Dispose();
}
catch (ArgumentException errArgument)
{
//The file was not a valid jpg file
lblPhotoErrors.Text = "One or more invalid files. (Not a jpg.)";
System.IO.File.Delete(Serv
}
//**End image part**