Sreedhar Danturthi
Sreedhar Danturthi

Reputation: 7601

Uploading a file using fileupload control in asp.net

I want to upload a file using FileUpload Control in asp.net and i'm using the following code to do that:

string filename1 = System.IO.Path.GetFileName(FileUploadControl.FileName);
FileUploadControl.SaveAs("C:\\Users\\admin\\Desktop\\ExperimentForFolder\\" + filename1);

i also tried

FileUploadControl.SaveAs(@"C:\Users\admin\Desktop\ExperimentForFolder\" + filename1);

But it is still freaking out. I don't understand what is wrong. Can you please help me.

Thanks in anticipation

Upvotes: 1

Views: 14635

Answers (2)

Muhammad Akhtar
Muhammad Akhtar

Reputation: 52241

Why do you need to save the file to your machine's Desktop?

Your ultimate option should be to use your Application Folder. It can be done like...

FileUpload1.SaveAs(Server.MapPath("~/AppFolderName/" + FileName));

Upvotes: 5

Shadow Wizzard
Shadow Wizzard

Reputation: 66397

You need permissions over the place you store the file into... don't store in the server desktop.

Try this for start:

FileUploadControl.SaveAs(Server.MapPath(filename1));

This will store the file in the same place as your .aspx file, if it works you can then create separate folder there then change the code to:

FileUploadControl.SaveAs(Server.MapPath("ExperimentForFolder/" + filename1));

Upvotes: 1

Related Questions