示例#1
0
        public ActionResult ShowImage(int type, string picid)
        {
            try
            {
                if (type > MongoDbNameArray.Length)
                {
                    return(Json(new JsonBaseEntity {
                        ErrorCode = "10000", ErrorMessage = "上传参数错误!"
                    }));
                }
                string mongodbName = MongoDbNameArray[type];
                if (string.IsNullOrEmpty(mongodbName))
                {
                    return(Json(new JsonBaseEntity {
                        ErrorCode = "10000", ErrorMessage = "上传参数错误!"
                    }));
                }

                if (!string.IsNullOrWhiteSpace(picid) && Regex.IsMatch(picid, @"[\w\W]+.[jpeg|gif|jpg|png|bmp|pic]", RegexOptions.IgnoreCase) && picid.Length > 32 && MongoDBHelper.ExistsFile(mongodbName, picid))
                {
                    try
                    {
                        byte[] filebytearray = MongoDBHelper.GetFileByteArrayByName(mongodbName, picid);
                        //return File(filebytearray, ContentTypeHelper.GetValue(Path.GetExtension(picid)), picid);//下载图片
                        return(File(filebytearray, ContentTypeHelper.GetValue(Path.GetExtension(picid))));
                    }
                    catch (Exception ex)
                    {
                        logger.ErrorFormat("function:{0},location:{1},params:id={2},Message={3},StackTrace={4},Source={5}", "GET", "MongoDBLegacyHelper.GetFileByteArrayByName", picid, ex.Message, ex.StackTrace, ex.Source);
                        return(new EmptyResult());
                    }
                }
                else
                {
                    string filename = string.IsNullOrEmpty(picid) ? "beijing1.jpg" : picid;
                    if (filename.Length > 32)
                    {
                        filename = "beijing1.jpg";
                    }
                    return(JavaScript("windows.loaction.href='" + string.Format("{0}/images/personalhomepage/big/{1}", CommonHelper.GetConfigValue("SourceAction"), filename) + "'"));
                }
            }
            catch (Exception ex)
            {
                logger.ErrorFormat("function:{0},location:{1},params:id={2},Message={3},StackTrace={4},Source={5}", "ShowImage", "MongoDBLegacyHelper.ExistsFile", picid, ex.Message, ex.StackTrace, ex.Source);
                return(new EmptyResult());
            }
            finally
            {
            }
        }
示例#2
0
        /// <summary>
        /// 根据文件名,获取一个ContentType
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string GetContentType(string fileName)
        {
            string contentType   = "";
            string extensionName = Path.GetExtension(fileName);

            if (string.IsNullOrEmpty(extensionName))
            {
                contentType = "";
            }
            else
            {
                contentType = ContentTypeHelper.GetValue(extensionName);
            }
            return(contentType);
        }