I am using FileUpload
control to handle the file in following scenario, I have downloaded
some files manually in my machine, now I need to select files one by one to process their data in Database
and Map those files to another location (server).
I have a method which do this functionality, which needs fully qualified path of that file to further process it. I have used fileUpload.PostedFile.FileName
which is only returning file name, not FQN.
if (fileUploadList.HasFile)
{
string source = ddActiveList.SelectedItem.Text;
string url = fileUploadList.PostedFile.FileName;
string name = "Name";
string id = "1";
bool res = SaveFileToLocalMachine(url, name, id, source);
}
private bool SaveFileToLocalMachine(string url, string fileName, string sourceId, string source)
{
bool flag = false;
lSource = new Object();
string savePath = "@\\myPath";
if (!Directory.Exists(savePath))
{
DirectoryInfo di = Directory.CreateDirectory(savePath);
}
using (var client = new WebClient())
{
try
{
string fileExt = System.IO.Path.GetExtension(url);
string file = fileName + fileExt;
client.DownloadFile(url, savePath + file);
lSource.Update();
flag = true;
}
catch (Exception ex)
{
flag = false;
}
}
return flag;
}