asp. net Method for remote uploading pictures based on Web Service

  • 2021-07-10 19:24:48
  • OfStack

This paper gives an example of how asp. net can upload pictures remotely based on Web and Service. Share it for your reference, as follows:

Page calling code: If you add Web reference,


HttpFileCollection files = HttpContext.Current.Request.Files;
string filePath = files[0].FileName;
string fileName = filePath.Substring(filePath.LastIndexOf("//") + 1);
byte[] datas = new byte[files[0].ContentLength];
System.IO.Stream fs;
localhost.WebService web = new localhost.WebService();
fs = (System.IO.Stream)files[0].InputStream;
// Read the input stream into 2 In the dimension array 
fs.Read(datas, 0, files[0].ContentLength);
fs.Close();
Response.Write(web.UploadFile(datas,fileName));

Code in Web Service


[WebMethod(Description=" Upload the server picture information and return whether it is successful or not ")]
public string UploadFile(byte[] fs,string fileName)
{
  // Create a memory flow   Write an array to a memory stream 
  MemoryStream memory = new MemoryStream(fs);
  // Write something from memory to the file stream 
  FileStream stream = new FileStream(HttpContext.Current.Server.MapPath(".") + "//images" + fileName,FileMode.Create);
  // Writes something from the memory stream to FileStream In stream 
  memory.WriteTo(stream);
  stream.Close();
  memory = null;
  stream = null;
  return " File uploaded successfully !";
}

I hope this paper is helpful to everyone's asp. net programming.


Related articles: