⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filemanageserivce.asmx.cs

📁 ASP C#代码实例 适合初学人士学习使用
💻 CS
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.IO;
using System.Drawing;

namespace Example_12_21
{
	/// <summary>
	/// FileManageSerivce 的摘要说明。
	/// </summary>
	public class FileManageSerivce : System.Web.Services.WebService
	{
		public FileManageSerivce()
		{
			//CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的
			InitializeComponent();
		}

		#region 组件设计器生成的代码
		
		//Web 服务设计器所必需的
		private IContainer components = null;
				
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if(disposing && components != null)
			{
				components.Dispose();
			}
			base.Dispose(disposing);		
		}
		
		#endregion

		/// <summary>
		/// Web 服务提供的方法,返回给定文件的字节数组。
		/// </summary>
		[WebMethod(Description="Web 服务提供的方法,返回给定文件的字节数组")]
		public byte[] GetImage(string requestFileName)
		{
			///得到服务器端的一个图片
			if(requestFileName != null || requestFileName != "")
			{
				return getBinaryFile(requestFileName);
			}
			else
				return new byte[0];
		}

		/// <summary>
		/// getBinaryFile:返回所给文件路径的字节数组。
		/// </summary>
		/// <param name="filename"></param>
		/// <returns></returns>
		public byte[] getBinaryFile(string filename)
		{
			byte[] filebyte = null;

			if(File.Exists(filename))
			{
				try
				{
					///打开现有文件以进行读取。
					FileStream s = File.OpenRead(filename);
					s.Read(filebyte,0,(int)s.Length);
					return(filebyte);
				}
				catch
				{
					return new byte[0];
				}
			}
			else
			{
				return new byte[0];
			}
		}

		[WebMethod(Description="Web 服务提供的方法,返回给定文件类型。")]
		public string GetImageType()
		{
			///这里只是测试,您可以根据实际的文件类型进行动态输出
			return "image/jpg";
		}

		[WebMethod(Description = "Web 服务提供的方法,返回是否文件上载成功与否。")]
		public string UploadFile(byte[] fs, string FileName)
		{
			try
			{
				///定义并实例化一个内存流,以存放提交上来的字节数组。
				MemoryStream m = new MemoryStream(fs);
				///定义实际文件对象,保存上载的文件。
				FileStream f = new FileStream(Server.MapPath(".") + "\\"
					+ FileName, FileMode.Create);
				///把内内存里的数据写入物理文件
				m.WriteTo(f);
				Bitmap bm = null;
				bm = new Bitmap(f);
				bm.Save(Server.MapPath(".") + "\\"
					+ FileName+".JPEG");
				m.Close();
				f.Close();
				f = null;
				m = null;
				return "文件已经上传成功。";
			}
			catch (Exception ex)
			{
				return ex.Message;
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -