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;
    }
    
    
  • 相关阅读:
    Flink 读取 Kafka 数据 (极简版)
    自动化测试模型
    C语言字符串处理库函数大全(转)
    c语言笔记
    c语言的自动类型转换(转)
    itest(爱测试)开源接口测试&敏捷测试管理平台8.1.0发布
    itest(爱测试)开源接口测试&敏捷测试&极简项目管理 8.0.0 发布,测试重大升级
    工控机折腾小记
    linux
    华为交换机服务端策略路由配置
  • 原文地址:https://www.cnblogs.com/cyapi/p/14203022.html
Copyright © 2011-2022 走看看