Click here to Skip to main content
15,915,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a simple aspx page which allows photo uploads, when I test the page through vis studio everything works fine however when I upload the page/code to my webhost (hostgator windows shared package) I am getting errors at the fileupload saveas line, the errors arent very specific, just telling me its a runtime error, code is below, can anyone shed some light on why this is happening from my host server but not with vis studio on my local machine?

C#
protected void btnUploadPhoto_Click(object sender, EventArgs e)
        {
            string imageFolder = "Media";
            string savePath;
            string saveFile;

            if (fileUploadPhoto.HasFile)
            {
                savePath = Path.Combine(Request.PhysicalApplicationPath, imageFolder);
                saveFile = Path.Combine(savePath, fileUploadPhoto.FileName);
                //lblConfirmPhotoUpload.Text = saveFile; // test line only
                fileUploadPhoto.SaveAs(saveFile);

                SqlDataSourcePhotos.Insert();
                gridViewPhotos.DataBind();
            }
        }
Posted
Comments
Orcun Iyigun 28-Feb-12 19:55pm    
also can you tell me that runtime error please?

Well, I believe in production you need to use Server.MapPath() for your path. This will solve your problem. So try adding your path into Server.MapPath() after combining them. For MSDN link look here[^].

[UPDATE]After OP's comment[/UPDATE]

it should be something like:
C#
string path = Server.MapPath(saveFile);
if(File.Exists(path)) // This is just to make sure if the file exists or not
{
  fileUploadPhoto.SaveAs(path);
}


Good luck,
OI
 
Share this answer
 
v2
Comments
Orcun Iyigun 28-Feb-12 19:45pm    
From OP:Hi thanks for the reply...

I still dont think I quite understand how to include the MapPath method in my code above, would be as kind as to provide me the code line I need to insert and where..

Ive tried this

<pre>
if (fileUploadPhoto.HasFile)
{
savePath = Path.Combine(Request.PhysicalApplicationPath, imageFolder);
saveFile = Path.Combine(savePath, fileUploadPhoto.FileName);
MapPath(saveFile);
//lblConfirmPhotoUpload.Text = saveFile; // test line only
fileUploadPhoto.SaveAs(saveFile);

SqlDataSourcePhotos.Insert();
gridViewPhotos.DataBind();
}
</pre>

but even looking at it for some reason doesnt seem right,
First of all, by simply encapsulating the method body in a try-catch block and printing the exception in case of an error should usually let you pin-point the location of your error.

Secondly, after going through your code, as Orcun directed you, please read upon Server.MapPath[^] method. It allows you to map a path like "~/images/" to physical disk paths.

Additionally, my best guess is that this is a file permission issue. The account under which your web server is run as, might not have permissions on your machine to create files on some locations. Check on that.

Hope this helps
 
Share this answer
 
use Server.MapPath to save file on server It may be possible on server you don't have access to specific folder:
C#
fileUploadPhoto.SaveAs(Server.MapPath("Images/..."));

This will help you.
 
Share this answer
 
v2
Firstly a huge thankyou to all that posted, after incorporating some try catch blocks (yeah i got lazy)

I found out that the exception unhandled was

Exception Details: System.UnauthorizedAccessException: Access to the path '(path removed)\httpdocs\Media\Image4.jpg' is denied.


after going back to my host with that information they cleared it up for me right away and I still was able to use my original code, apparently it as stated above was a folder permission setting

once again thanks for all the help
 
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