zoukankan      html  css  js  c++  java
  • C# 接收前端上传图片

    //传的是我用户id(perid)
    public static string UploadUid(int perid) {
    	//path为你当前项目下的路径,   perimage为存储图片的文件夹
    	string path = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "perimage/";
    	var fileNewName = "";
    	// 如果目录不存在则要先创建
    	if (Directory.Exists(path)) {
    		//string path = "D:\freehost\mxcjobcn\web\perimage/";
    		//用户提交的数据
    		//var Data = System.Web.HttpContext.Current.Request.Form;
    		string filesrc = "";
    		//获取上传的文件
    		var httpPostedFile = HttpContext.Current.Request.Files;
    		if (httpPostedFile != null && httpPostedFile.Count > 0) {
    			var file = httpPostedFile[0];
    			string imgType = Path.GetExtension(file.FileName);
    			//限制文件上传类型
    			if (imgType.Contains(".jpg") || imgType.Contains(".png") || imgType.Contains(".bmp") || imgType.Contains(".jpeg") ||
    				imgType.Contains(".gif")) {
    				//返回给前端一个名字用来搜索图片地址并展示
    				fileNewName = perid + DateTime.Now.ToString("yyyyMMddHHmmss") + imgType;
    				filesrc = path + fileNewName;
    				file.SaveAs(filesrc);
    			}
    		}
    	} else {
    		string filesrc = "";
    		//获取上传的文件
    		var httpPostedFile = HttpContext.Current.Request.Files;
    		if (httpPostedFile != null && httpPostedFile.Count > 0) {
    			var file = httpPostedFile[0];
    			string imgType = Path.GetExtension(file.FileName);
    			//限制文件上传类型
    			if (imgType.Contains(".jpg") || imgType.Contains(".png") || imgType.Contains(".bmp") || imgType.Contains(".jpeg") ||
    				imgType.Contains(".gif")) {
    				fileNewName = perid + DateTime.Now.ToString("yyyyMMddHHmmss") + imgType;
    				filesrc = path + fileNewName;
    				file.SaveAs(filesrc);
    			}
    		}
    	}
    	return fileNewName;
    }
    
    
  • 相关阅读:
    2017.7.18 linux下ELK环境搭建
    2017.7.18 linux下用户、组和文件的操作
    action解耦方式
    action 耦合方式
    分页查询
    request,response,session
    Struts2
    hibernate中session,HQL,持久化,一对多,多对一
    hibernate 配置文件
    hibernate
  • 原文地址:https://www.cnblogs.com/cyapi/p/14203022.html
Copyright © 2011-2022 走看看