• C#保存上传来的图片示例代码


    保存上传图片的方法有很多,在接下来的文章中为大家详细介绍下使用C#是如何做到的,感兴趣的朋友不要错过
    复制代码代码如下:

    [HttpPost] 
    public string UploadImage() 

    //string ss = Request.Form["uploadFile"]; 
    //return ss; 
    HttpPostedFileBase uploadFile = Request.Files[0]; 
    string fileName = uploadFile.FileName; 
    int fileSize = uploadFile.ContentLength; 
    string fileExt = Path.GetExtension(fileName).ToLower(); 
    string message = ""; 
    if (!(fileExt == ".png" || fileExt == ".gif" || fileExt == ".jpg" || fileExt == ".jpeg")) 

    message = "图片类型只能为gif,png,jpg,jpeg"; 
    return message; 

    else 
    { // www.jbxue.com
    if (fileSize > (int)(500 * 1024)) 

    message = "图片大小不能超过500KB"; 
    return message; 

    else 

    Random r = new Random(); 
    string uploadFileName = DateTime.Now.ToString("yyyyMMddhhmmss") + r.Next(100000, 999999) + fileExt; 
    try 

    string directoryPath = Server.MapPath("~/UploadImages/"); 
    if (!Directory.Exists(directoryPath))//不存在这个文件夹就创建这个文件夹 

    Directory.CreateDirectory(Server.MapPath("~/UploadImages/")); 

    uploadFile.SaveAs(Server.MapPath("~/UploadImages/") + uploadFileName); 
    message = uploadFileName; 
    return message; 

    catch (Exception ex) 

    message = ex.Message; 
    return message; 



  • 相关阅读:
    Android流畅度测试
    linux常用操作指令
    SQL语句
    客户端专项测试谈
    我的面经(ing)
    整理面试题
    百度质量部测试开发面试题
    UIResponder响应链
    NSURLSession进行网络请求
    命令行工具打包
  • 原文地址:https://www.cnblogs.com/linuxnotes/p/3415305.html
Copyright © 2020-2023  润新知