Solved

image upload in C#

Posted on 2010-09-14
8
363 Views
Last Modified: 2012-05-10
I'm trying to upload image into SQL server database.
What's important is that i'm not really uploading image itself, but path to the location of the picture
I'll save the picture in designated location (in this case let's say it's c:\images\), and have imageURL
saved like \images\picture1.jpg

I'd like to set a limit so that people don't upload anything but jpg, bmp, gif, png

I'll post up what I have and please help ASAP code.txt code.txt
form.txt
0
Comment
Question by:kmc10314
[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
  • 6
  • 2
8 Comments
 
LVL 5

Expert Comment

by:priyananth
ID: 33675787
string extension = Path.GetExtension(fileUpload.PostedFile);

if (extension  == ".jpg" || extension  == ".jpg" || ".bmp" ) // and other types
{
  proceed....
}
else
{
show  error message
}
0
 

Author Comment

by:kmc10314
ID: 33682297
this gives me error of
cannot convert system.web.httppostedfile to string
0
 

Author Comment

by:kmc10314
ID: 33682381
well i got that problem fixed, but i still need help with saving a picture to a location like c:\images\
and saving the path as string.

My method does not even store the filename seems to be null
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 5

Accepted Solution

by:
priyananth earned 500 total points
ID: 33683981
I missed one property in

string extension = Path.GetExtension(fileUpload.PostedFile);

Replace the above with below :

string extension = Path.GetExtension(fileUpload.PostedFile.FileName);
0
 

Author Comment

by:kmc10314
ID: 33684457
i did. Problem is now that it's not getting any file even though i upload it.
It does not even show any message
I already uploaded aspx file up there and I'm going to attach code snippet for my behind code.
it seems like the it has no file even though I choose the file.
if (fileUpload.PostedFile.ContentLength > 0)
        {
            string extension = Path.GetExtension(fileUpload.PostedFile.FileName);
            if (extension == ".jpg" || extension == ".jpeg" || extension == ".jpeg" || extension == ".png" || extension == ".bmp" || extension == ".gif")
            {
                try
                {
                    string fileName = System.IO.Path.GetFileName(fileUpload.PostedFile.FileName);
                    string savePath = "c:\\images\\";
                    fileUpload.PostedFile.SaveAs(savePath);
                    Session["filename"] = fileName;
                    Response.Write("File Uploaded");
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }
            }
            else
            {
                Response.Write("Please upload jpg/jpeg, bmp, gif, png file only");
            }
        }
            
        else
        {
            Response.Write("You have no file.");
        }

Open in new window

0
 

Author Comment

by:kmc10314
ID: 33684574
Now.. I think there is something wrong with this part


try
                {
                    string fileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
                    string savePath = "c:\\images\\";
                    FileUpload1.PostedFile.SaveAs(savePath);
                    Session["filename"] = fileName;
                    Response.Write("File Uploaded");
                }

Open in new window

0
 

Author Comment

by:kmc10314
ID: 33684658
when i submit the file i get this error

The filename, directory name, or volume label syntax is incorrect.
0
 

Author Closing Comment

by:kmc10314
ID: 33685402
priyanath helped me to restrict the extension, but most of part i had to take care of the problem by myself.
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Reformat SQL - so SSRS can read the columns 25 33
ASP sending two datas 2 23
SQL server client app 3 32
P3P validator , ASP, PHP ,Https and Http links 3 12
I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
I was asked about the differences between classic ASP and ASP.NET, so let me put them down here, for reference: Let's make the introductions... Classic ASP was launched by Microsoft in 1998 and dynamically generate web pages upon user interact…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 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