Skip to content

Instantly share code, notes, and snippets.

@yannvery
Last active January 4, 2016 05:59
Show Gist options
  • Select an option

  • Save yannvery/8578948 to your computer and use it in GitHub Desktop.

Select an option

Save yannvery/8578948 to your computer and use it in GitHub Desktop.
Upload file web method with c#
public bool UploadFile(string FileName, byte[] buffer, long Offset)
{
bool retVal = false;
try
{
// setting the file location to be saved in the server.
// reading from the web.config file
string FilePath =
//Path.Combine(ConfigurationManager.AppSettings["upload_path"], FileName);
//WebConfigurationManager.AppSettings["PFUserName"].ToString()
Path.Combine(System.Web.Hosting.HostingEnvironment.MapPath
("~/TransientStorage/"), FileName);
;
if (Offset == 0) // new file, create an empty file
File.Create(FilePath).Close();
// open a file stream and write the buffer.
// Don't open with FileMode.Append because the transfer may wish to
// start a different point
using (FileStream fs = new FileStream(FilePath, FileMode.Open,
FileAccess.ReadWrite, FileShare.Read))
{
fs.Seek(Offset, SeekOrigin.Begin);
fs.Write(buffer, 0, buffer.Length);
}
retVal = true;
}
catch (Exception e)
{
//sending error to an email id
}
return retVal;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment