Skip to content

Instantly share code, notes, and snippets.

@weeksdev
Created January 21, 2014 16:33
Show Gist options
  • Select an option

  • Save weeksdev/8543353 to your computer and use it in GitHub Desktop.

Select an option

Save weeksdev/8543353 to your computer and use it in GitHub Desktop.
Upload File with Asmx
[WebMethod]
public void UploadFile()
{
try
{
//HTTP Context to get access to the submitted data
HttpContext postedContext = HttpContext.Current;
//File Collection that was submitted with posted data
HttpFileCollection Files = postedContext.Request.Files;
//Designate the path to save the file on the server.
string serverSavePath = @"\Path\On\Server\";
//Get the filename from the request data.
string fileName = (string)postedContext.Request.Files[0].FileName;
if (fileName != null && fileName != "")
{
//The byte array we'll use to write the file with
byte[] binaryWriteArray = new
byte[Files[0].InputStream.Length];
//Read in the file from the InputStream
Files[0].InputStream.Read(binaryWriteArray, 0,
(int)Files[0].InputStream.Length);
//Open the file stream
System.IO.FileStream objfilestream = new System.IO.FileStream( serverSavePath + fileName, System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite);
//Write the file and close it
objfilestream.Write(binaryWriteArray, 0,
binaryWriteArray.Length);
objfilestream.Close();
}
else
{
//An error occured with the data for the files received.
}
}
catch (Exception ex1)
{
//An error occured with the process to save the file to the server.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment