Solved

upload file to folder and insert to  db

Posted on 2007-03-21
3
506 Views
Last Modified: 2008-01-09
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:JMIConnectionString %>"
                       InsertCommand="INSERT INTO [Clientbrief] ([reserach]) VALUES (@reserach)"
   
                                   
                       <InsertParameters>
       
                           <asp:FormParameter FormField="Reserach" Name="Reserach" Type="String"
                               />
                   Type="String" />
                       </InsertParameters>
                   </asp:SqlDataSource>
0
Comment
Question by:gianitoo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 2

Expert Comment

by:Ehrick
ID: 18766290
Assuming that you are uploading a file selected by a user from an asp.net 2 page, you'd do the following:

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.ContentLength>0)
            {
                if (System.IO.Path.GetExtension(uploadedImageFile.FileName).ToLower() != ".jpg" &&
                    System.IO.Path.GetExtension(uploadedImageFile.FileName).ToLower() != ".gif")
                {
                    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.InputStream);

                    //save the image with a unique file name
                    string uploadFileName = System.IO.Path.GetFileNameWithoutExtension(uploadedImageFile.FileName) + ".jpg";
                    int fileAppendNum = 0;
                    while (System.IO.File.Exists(Server.MapPath("Images/" + uploadFileName)))
                    {
                        fileAppendNum++;
                        uploadFileName = System.IO.Path.GetFileNameWithoutExtension(uploadedImageFile.FileName) +
                            fileAppendNum.ToString() + ".jpg";
                    }
                   
                    uploadedPhoto.Save(Server.MapPath("uploadedImages/" + uploadFileName),
                        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(Server.MapPath("uploadedImages/" + fileName));
                }
                //**End image part**
0
 
LVL 1

Author Comment

by:gianitoo
ID: 18766457
can u send it in vb.net
0
 
LVL 2

Accepted Solution

by:
Ehrick earned 500 total points
ID: 18766848
I'm not really familiar with vb.net. However, another expert may be able to do the translation, or you may be able to find appropriate substitutes yourself. I put the code into a c# to vb.net translator at http://authors.aspalliance.com/aldotnet/examples/translate.aspx. You might try this and see if it works:

Dim uploadedImageFile As HttpPostedFile = Request.Files(0)
If uploadedImageFile.ContentLength > 0 Then
   If System.IO.Path.GetExtension(uploadedImageFile.FileName).ToLower() <> ".jpg" And System.IO.Path.GetExtension(uploadedImageFile.FileName).ToLower() <> ".gif" Then
      lblPhotoErrors.Text = "One or more files that you selected are not JPEG or GIF format."
      Return
   End If
   
   '**Get the image**
   Try
      Dim uploadedPhoto As New Bitmap(uploadedImageFile.InputStream)
     
      'save the image with a unique file name
      Dim uploadFileName As String = System.IO.Path.GetFileNameWithoutExtension(uploadedImageFile.FileName) + ".jpg"
      Dim fileAppendNum As Integer = 0
      While System.IO.File.Exists(Server.MapPath(("Images/" + uploadFileName)))
         fileAppendNum += 1
         uploadFileName = System.IO.Path.GetFileNameWithoutExtension(uploadedImageFile.FileName) + fileAppendNum.ToString() + ".jpg"
      End While
     
      uploadedPhoto.Save(Server.MapPath(("uploadedImages/" + uploadFileName)), ImageFormat.Jpeg)
     
      'TODO: Write uploadFileName to your sql database
      'Destroy objects. This may not be necessary.
      uploadedPhoto.Dispose()
   Catch errArgument As ArgumentException
      'The file was not a valid jpg file
      lblPhotoErrors.Text = "One or more invalid files. (Not a jpg.)"
      System.IO.File.Delete(Server.MapPath(("uploadedImages/" + fileName)))
   End Try
End If '**End image part**
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question