Click here to Skip to main content
15,881,725 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Trying to Upload and Image.

C#
if (FileUpload1.HasFile)
            {
                string imagename = System.IO.Path.GetFileName(FileUpload1.FileName);
                string ext = System.IO.Path.GetExtension(FileUpload1.FileName);
                string imagefile = Server.MapPath("Pictures/" + imagename + ext);
                byte[] Image = null;
                if (ext == ".jpg" | ext == ".gif" | ext == ".png")
                {
                    Image = new byte[FileUpload1.PostedFile.ContentLength];
                    HttpPostedFile UploadedImage = FileUpload1.PostedFile;
                    UploadedImage.InputStream.Read(Image, 0, (int)FileUpload1.PostedFile.ContentLength);
                    int numBytesToRead = Image.Length;
                    try
                    {
                        using (FileStream fsNew = new FileStream(imagefile, FileMode.Create, FileAccess.Write))
                        {
                            fsNew.Write(Image, 0, numBytesToRead);
                        }
                    }
                    catch (FileNotFoundException ioEx)
                    {
                        Label100.Text = "Filestream Add Pictures: " + ioEx.ToString();
                    }
                }


What I have tried:

Tried just using the filename into pictures. Did not work
Posted
Updated 12-Jul-17 16:24pm
v2
Comments
j snooze 12-Jul-17 17:29pm    
did you get an error of any kind?
j snooze 12-Jul-17 17:35pm    
This worked for me(just have to change where I have the [MyDestinationPath]

private string Upload(HttpPostedFileBase file)
{
string path = string.Empty;

try
{
if ((file != null) && (file.ContentLength > 0) && !string.IsNullOrEmpty(file.FileName))
{
FileInfo filepath = new FileInfo(file.FileName);
string fileName = file.FileName;
string fileContentType = file.ContentType;
byte[] fileBytes = new byte[file.ContentLength];
file.InputStream.Read(fileBytes, 0, Convert.ToInt32(file.ContentLength));
path = string.Concat([MyDestinationPath], filepath.Name);
file.SaveAs(Server.MapPath(path));

return path;
}
}
catch (Exception ex)
{

}

return path;
}
F-ES Sitecore 13-Jul-17 4:38am    
"Doesn't work" does not give anyone enough information to help you. Do you phone a mechanic and say "my car doesn't work, how do I fix it?"

Debug your code and step through it to get a better understanding of what it is doing\not doing, then provide that information in the question along with any error messages you get and on what line they occur.

1 solution

Check the permissions to the directory you are trying to save the file to.
The account running the web-site will not have write permissions to the file system by default, if it did you would be able to upload any type of file to the website.

Kind Regards
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900