HttpPostedFile 和 HttpPostedFileBase 你真的了解嘛?

     当你看到的时候,你是不是已经爱上了它,如果你真的只看外表,那你就错了,不要太相信自己的眼睛,往往真像并不是你所看到的那么简单!请跟我一起来看看吧!

     这次在项目中,就遇到了这个问题,刚开始我还天真的以为他们真的有关系,没有到都是假象的。

     遇到的问题:“把图片上传到资源服务器”的一个上传问题,刚开始做的时候没考虑到,代码如下:

    

public bool UploadFTP(HttpPostedFileBase file, string strFileType, int iFileLength, int Width, int Height, string Path, ref string strInfo)
{
   ............. //     
}


     本以为这样就已经是通用的了,当我直接传HttpPostedFile 对象的时候报错了。答案是 HttpPostedFile HttpPostedFileBase 不存在关系。

     所以我只好,到晚上去找,去查,终于找到了一个好的解决方案,其实它们还是可以通过一个桥梁 HttpPostedFileWrapper 类来转化,HttpPostedFileWrapper : HttpPostedFileBaseHttpPostedFileWrapper 的代码如下:

public class HttpPostedFileWrapper : HttpPostedFileBase
{
        // 摘要:
        //     初始化 System.Web.HttpPostedFileWrapper 类的新实例。
        //
        // 参数:
        //   httpPostedFile:
        //     通过此包装类可访问的对象。
        //
        // 异常:
        //   System.ArgumentNullException:
        //     httpApplicationState 为 null。
        public HttpPostedFileWrapper(HttpPostedFile httpPostedFile);
}

最后解决方案如下:

 public bool UploadFTP(HttpPostedFile file, string strFileType, int iFileLength, int Width, int Height, string Path, ref string strInfo)
{ HttpPostedFileBase hpfb
= new HttpPostedFileWrapper(file) as HttpPostedFileBase; return UploadFTP(hpfb, strFileType, iFileLength, Width, Height, Path, ref strInfo); }

 每天进步一点点,一年累积的进步,你去好好想想吧!

 您的支持是我最大的动力,如果你觉得还可以,请您给个”推荐“!

转载于:https://www.cnblogs.com/Kummy/archive/2013/02/27/2934608.html

猜你喜欢

转载自blog.csdn.net/weixin_33958585/article/details/93428299