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

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

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

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

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

所以我只好,到晚上去找,去查,终于找到了一个好的解决方案,其实它们还是可以通过一个桥梁 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);
}

本文转载:CSDN博客