# Issue with saving image file onto server from client machine

I am using the below code to Upload an Image file to a SharePoint Document Library. The code works fine locally but once deployed to server, i get the Exception as file not found.

                String fileToUpload = FlUpldImage.PostedFile.FileName; //@"C:\Users\admin.RSS\Desktop\Photos\me_skype.jpg";
String documentLibraryName = "SiteAssets";

SPFolder myLibrary = web.Folders[documentLibraryName];

Boolean replaceExistingFiles = true;
String fileName = CheckStringNull(txtFirstName.Text) + CheckStringNull(txtLastName.Text) + CheckDateNull(txtDOB) + System.IO.Path.GetFileName(fileToUpload); ;
if (fileName.Contains('/'))
{
fileName = fileName.Replace("/", "");
}
if (fileName.Contains(':'))
{
fileName = fileName.Replace(":", "");
}
SPFile spfile = myLibrary.Files.Add(fileName, fileStream, replaceExistingFiles);
string url = site.ToString() + "/" + spfile.ToString();
if (url.Contains("="))
{
url = url.Split('=')[1];
}
//Commit
myLibrary.Update();


The string fileupload contains URL as C:\Users\admin.RSS\Desktop\Photos\me.jpg This URL is actually the client system and the server side code throws exception as file not found. How to handle this issue?

UPDATE:

If i try withot the lines of code that checks if the file exists i.e withoutFileStream fileStream = File.OpenRead(fileToUpload); than i get the exception as as c:\windows\system32\inetsrv\me.jpg cold not be found

I want to upload a file from client machine onto server.How can i do this kindly help, i am facing issue with Path related stuff. Kindly help. Thank You

-
I also happened that problem. Still cannot figure out. I am using sharepoint:attachment field to upload the file. So that issue is solved? –  user19187 Aug 28 '13 at 15:23
You need to upload files onto server first and than from there upload to document library.string Virtualpath = Server.MapPath("\\"); if (!(Directory.Exists(Virtualpath + @"Images"))) SPSecurity.RunWithElevatedPrivileges(delegate() { Directory.CreateDirectory(Virtualpath + @"Images");}); and than use custom code to save file onto server. –  Ishan Aug 29 '13 at 6:14

To save the file you first need to get it to the server. You are now trying to open a file on the server using the path supplied by the client, basically your looking for the clients file on the server... Kind of explains why it works locally. If your using a ASP.Net FileUPload control one example is to do like this.

   if(FileUpload1.HasFile)
{
byte[] input = new byte[fileLen - 1];

using (MemoryStream memory = new MemoryStream(input))
{
}

}

-
mypath is the path from FileUpload and which is local path, if i access file from remote location than i get the error as file not found. How can i handle this –  Ishan Aug 10 '12 at 4:44
So changed, removed the file name variable to not confuse. You can not(!) open a location on the client from the server, in this scenario. Above you have an example of how you get the file to the server, and have it stored in memory. Now you can save it to a library, to server disk or do what ever you like as shown by the comment in the using statement. If you want the file name of the uploaded file you can use the FileName property on the FileUpload control. –  Robban1980 Aug 10 '12 at 5:05

Check out this link. Make sure you are using the client api correctly. Also notice the different ways of uploading your file depending on how big the file is.

-
While this may theoretically answer the question, we prefer inclusion of the essential parts of the answer here, and to provide the link for reference. See answer for general guidelines. –  SPDoctor Aug 29 '13 at 7:34

Why not use just an HTTP PUT?

string filepath = ...
WebRequest request = WebRequest.Create("[Document Library URL]" + "/" + filename);
//Set crediential - either create or use existing
request.Credentials = CredentialCache.DefaultCredentials;
request.Method = "PUT";
byte[] buffer = new byte[1024];
using (Stream stream = request.GetRequestStream())
{
using (FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read))
{
for (int i = fs.Read(buffer, 0, buffer.Length); i > 0; i = fs.Read(buffer, 0, buffer.Length))
{
stream.Write(buffer, 0, i);
}
}
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
response.Close();
if (response.StatusCode == System.Net.HttpStatusCode.Created)
{
//OK
}
else
{
//Problem
}

-
My main issue is filepath, i used the code u mentioned and set it as string filepath = Convert.ToString(FlUpldImage.PostedFile.FileName); I am getting exception on line using (FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read)) as file not found as i am uploading the file from client system. –  Ishan Aug 9 '12 at 12:30