How to upload file from client pc to remote server - c#

I have developed a web application in asp.net c# , I want to upload a file from client machine to server.I have used to the following code
string path = Convert.ToString(ConfigurationManager.AppSettings["DocumentPath"]);
FileUpload1.SaveAs(Server.MapPath(path) + FileUpload1.FileName);

Try:
string path = Convert.ToString(ConfigurationManager.AppSettings["DocumentPath"]);
string strImageName3 = DateTime.Now.ToString("yyyyMMddHHmmssfff") + System.IO.Path.GetFileName(FileUpload1.FileName);
string strFilePath3 = Server.MapPath(path);
FileUpload1.SaveAs(strFilePath3 + strImageName3);
this DateTime.Now.ToString("yyyyMMddHHmmssfff") for adding yyyyMMddHHmmssfff string to the uploaded file name for unique uploaded filename.

Related

c# file copy to a server

I want to copy a pdf to my own server but i get an error about path.
the codes like that:
string filepath = System.IO.Path.GetFullPath(open.FileName);
when i pick the pdf I want to save it to my server with this codes:
string filename = "New name" + ".pdf";
string newpath = #"\\WDMyCloud\Certificates\" + filename;
File.Copy(filepath, newpath, true);
But when i run this code i take an error like "invalid path name.

Uploading files from a network share

I am trying copy a file from a network share (drive letter\folder) to a SharePoint document library using asp.net (C#) when I run the code locally and select a drive letter to copy from everything works fine, but when I publish it to IIS I get an error - Could not find a part of the path 'L:\Test\Test.Doc'. I am guessing this is because the drive mapping doesn't exist from IIS.
The code to get the file is:
if (FileUpload1.HasFile)
{
string spSite = System.Configuration.ConfigurationManager.AppSettings["SharePointSite"];
string RelativePath = System.Configuration.ConfigurationManager.AppSettings["DocumentLibraryRelativePath"];
SharePointIntegration sp = new SharePointIntegration();
string fileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName);
string fileName = Path.GetFileNameWithoutExtension(FileUpload1.PostedFile.FileName);
string filePath = Path.GetDirectoryName(FileUpload1.PostedFile.FileName) + "\\" + fileName + fileExtension;
sp.UploadDocument(spSite, RelativePath, "Documents", filePath, fileName, recordID.ToString(), (string)HttpContext.Current.Session["UserName"], fileExtension);
GetAttachments();
}
I am using the SaveBinaryDirect method to save the file in SharePoint - sp.UploadDocument(spSite, RelativePath, .......).
What do I need to change to get the file when it is running on IIS?

File uploading into server

I am making a website having image upload module.In my localhost server its working perfectly. That means i can upload images and save it. But when i host my solution i am getting a error. That is, Access to the path is denied.
This is the code i have used...
string fileName = FileUpload1.FileName.ToString();
string uploadFolderPath = "~/up_foto/";
string filePath = HttpContext.Current.Server.MapPath(uploadFolderPath);
FileUpload1.SaveAs(filePath + "\\" + fileName);`
What the wrong in this.. Please help me....
Thanks in advance....
I am afraid there's nothing wrong with your code, if it runs locally. Instead, you have to make sure if on the host environment the user "IUSER", or "IIS_IUSER", or the like, has access (Read/Write) to the upload folder.
Since you are getting "Access to the path is denied", Did you check the folder you are trying to upload is having write access
you can use Path.combine or server.mappath (dont forget to add System.IO in the namespaces)
string fileName = FileUpload1.FileName.ToString();
string uploadFolderPath = "~/Uploads/Images/";
string filePath1 = Server.MapPath(uploadFolderPath + fileName);
or
string fileName = FileUpload1.FileName.ToString();
string uploadFolderPath = "~/Uploads/Images/";
string filePath = Server.MapPath(uploadFolderPath);
string filePath1= Path.Combine(filepath1 + fileName);

Get Path in asp.net for c#

I am trying to get a path for a file which a users uploads but the path that I am getting is wrong because the path should be for an example
"C:\\Users\\Tranga.Patel\\Downloads\template.xlsx"
but on my program I get
"c:\\windows\\system32\\inetsrv\\Template Final.xlsx"
the code that I am using is
fileName = Path.GetFullPath(fileUpload.PostedFile.FileName);
I also tried using
fileName = Server.MapPath(Path.GetFullPath(fileUpload.PostedFile.FileName));
this give the directory of the project
try using following:
var path=Server.MapPath('Uploads folder path from root directory');
This gives you the folder path from the root directory of your website.
EDIT:- You should be knowing to which path users are saving the file if it is not in your site directory tree.
Are you using a File Upload control? If you are, you just need them to select the document they want to upload and then specify the path you want to save it at. For example
// Get File Name
documentName = Path.GetFileName(fuContentDocuments.FileName);
// Specify your server path
string serverPath = Server.MapPath("../../" + WebConfigurationManager.AppSettings["FilePath"].ToString());
// The final path
string fileLocation = (serverPath + "\\" + userId + "\\" + documentName);
// if folder doesn't exist then create it
if (!Directory.Exists(serverPath + "\\" + userId + "\\"))
{
// create the folder for the file
Directory.CreateDirectory(serverPath + "\\" + userId + "\\");
}
// Upload the file
fuContentDocuments.SaveAs(fileLocation);
Note: UserId is just the users login userId. This way other users wont override it.

How to download a file in IIS?

I have written two methods such as FileUpLoad() and FileDownLoad() to Upload and Download a single file in my local system.
void FileUpLoad()
{
string hfBrowsePath = fuplGridDocs.PostedFile.FileName; //fuplGridDocs is a fileupload control
if (hfBrowsePath != string.Empty)
{
string destfile = string.Empty;
string FilePath = Path.Combine(#"E:\Documents\");
FileInfo FP = new FileInfo(hfBrowsePath);
hfFileNameAutoGen.Value = PONumber + FP.Extension;
destfile = FilePath + hfFileNameAutoGen.Value; //hfFileNameAutoGen is a hidden field
fuplGridDocs.PostedFile.SaveAs(destfile);
}
}
void FileDownLoad(LinkButton lnkFileName)
{
string filename = lnkFileName.Text;
string FilePath = Path.Combine(#"E:\Documents", filename);
fuplGridDocs.SaveAs(FilePath);
FileInfo fileToDownLoad = new FileInfo(FilePath);
if (fileToDownLoad.Exists)
{
Process.Start(fileToDownLoad.FullName);
}
else
{
lblMessage.Text = "File Not Saved!";
return;
}
}
While running the application before hosting it in IIS, I can upload a file to the desired location and can also retrieve a file from the saved location. But after publishing it in the localhost, I can only Upload a file. I could not download the saved file. There is no exception too. The Uploaded file is saved in the desired location. I don't know why it is not retrieving the file? Why I cant download the file in IIS? I have searched a lot in the internet, but couldn't find the solution. How to solve this? I am using Windows XP and IIS 5.1 version.
How do you expect your Web Application to do a Process.Start when you deploy this site to a server, your just going to be opening pictures on the server, not on the client PC.
I think this will answer your question: http://www.codeproject.com/Articles/74654/File-Download-in-ASP-NET-and-Tracking-the-Status-o
Also the download file is missing a slash after E:\Documents
another option is to add your wildcard to IIS MIME types

Categories

Resources