HttpPostedFile 和 HttpPostedFileBase 你真的了解嘛?

来源:互联网 发布:大数据为什么在贵州 编辑:程序博客网 时间:2024/05/18 13:06

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);}

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

可以参考:http://social.msdn.microsoft.com/Forums/vstudio/en-US/3a022806-87a3-4c29-90df-a39bda502ff0/httpcontexthttprequesthttpresponsehttppostedfile-?forum=visualcshartzhchs


0 0
原创粉丝点击