上传图片 copy 到另一文件夹

逻辑是错误的 主要是里面取路径的方法,有两种,一种直接替换,一种动态截取

string subFilePath = filePath.Substring(3, subFilePathIndex + 3);

这里第一个 3 替换成 磁盘路径下第几个

 

    private string uploadImage(string pImageNewName, string pFolder)
    {
        string str = "";
        string mPath = pFolder + "/BuyInfo" + "/" + ViewState["CID"].ToString() + "/";  //string pFolder = "images";  //mPath = "images/BuyInfo/6/"
 
        string fileContentType = fldBuyInfoImage.PostedFile.ContentType;  // fileContentType = "image/pjpeg"
        if (fileContentType == "image/gif" || fileContentType == "image/jpg" || fileContentType == "image/jpeg" || fileContentType == "image/pjpeg" || fileContentType == "image/png" || fileContentType == "image/bmp" || fileContentType == "image/x-png")
        {
            if (fldBuyInfoImage.PostedFile.ContentLength > 0)
            {
                #region
                HttpPostedFile mImg = fldBuyInfoImage.PostedFile; 
                System.IO.Stream mImgStr = mImg.InputStream;
                System.Drawing.Image mImage = System.Drawing.Image.FromStream(mImgStr);
 
                int mWidth = mImage.Width;
                int mHeight = mImage.Height;
                int mNWidth = 150, mNHeith = 113;
                //string FileName=  "C:\Users\Public\Pictures\Sample Pictures\Hydrangeas.jpg"
                int mStart = fldBuyInfoImage.PostedFile.FileName.LastIndexOfAny("\\".ToCharArray()); //mStart = 40
                string mName = fldBuyInfoImage.PostedFile.FileName.Substring(mStart + 1); //mName = "Hydrangeas.jpg"
                string[] mStrName = mName.Split('.'); //mStrName[0] = "Hydrangeas"       mStrName[1] = "jpg"
 
                string mPhysicalPath = HttpContext.Current.Server.MapPath(mPath);//mPhysicalPath = "D:\\aMainProject\\Nearway\\images\\BuyInfo\\6\\";
                if (!Directory.Exists(mPhysicalPath))
                {
                    Directory.CreateDirectory(mPhysicalPath);
                }
 
                string mPhysicalPath2 = mPhysicalPath.ToLower().Replace("nearway\\images", "NearWay_Oversea\\PubImage");//"d:\\amainproject\\NearWay_Oversea\\PubImage\\buyinfo\\6\\"
                if (!Directory.Exists(mPhysicalPath2))
                {
                    Directory.CreateDirectory(mPhysicalPath2);
                }
 
                string mPhysicalName = HttpContext.Current.Server.MapPath(mPath + pImageNewName + '.' + mStrName[1]); //mPhysicalName ="D:\\aMainProject\\Nearway\\images\\BuyInfo\\6\\2042201306040904022082_big_4361.jpg"
                #endregion
                string newFilePath = Server.MapPath(mPath + pImageNewName + '.' + mStrName[1]); //newFilePath = "D:\\aMainProject\\Nearway\\images\\BuyInfo\\6\\2042201306040949407366_big_7095.jpg"
                newFilePath = newFilePath.ToLower().Replace("nearway\\images", "NearWay_Oversea\\PubImage");//newFilePath = "d:\\amainproject\\NearWay_Oversea\\PubImage\\buyinfo\\6\\2042201306040904022082_big_4361.jpg"
 
                if (File.Exists(mPhysicalName))
                {
                    #region
                    this.deleteOldImage(mPhysicalName);
                    fldBuyInfoImage.PostedFile.SaveAs(mPhysicalName);
                    str = mPath + pImageNewName + '.' + mStrName[1];
 
                    string filePath = Server.MapPath(mPath + pImageNewName + '.' + mStrName[1]);
                    int subFilePathIndex = filePath.IndexOf(pFolder);
                    string subFilePath = filePath.Substring(3, subFilePathIndex + 5);
                    filePath = filePath.Replace(subFilePath, "NearWay_Oversea\\PubImage");
 
                    string mFilePath = filePath.Replace(pImageNewName + '.' + mStrName[1], "");
                    if (!Directory.Exists(mFilePath))
                        Directory.CreateDirectory(mFilePath);
                    File.Copy(mPhysicalName, newFilePath, true);
                    str = mPath + pImageNewName + '.' + mStrName[1];
                    #endregion
                }
                else
                {
                    fldBuyInfoImage.PostedFile.SaveAs(mPhysicalName);
 
                    string filePath = Server.MapPath(mPath + pImageNewName + '.' + mStrName[1]);  //filePath = "D:\\aMainProject\\Nearway\\images\\BuyInfo\\6\\2042201306040904022082_big_4361.jpg"
                    int subFilePathIndex = filePath.IndexOf(pFolder);//subFilePathIndex =24
                    string subFilePath = filePath.Substring(3, subFilePathIndex + 3);// subFilePath = "aMainProject\\Nearway\\images"
                    filePath = filePath.Replace(subFilePath, "NearWay_Oversea\\PubImage");//filePath = "D:\\NearWay_Oversea\\PubImage\\BuyInfo\\6\\2042201306040904022082_big_4361.jpg"
                    string mFilePath = filePath.Replace(pImageNewName + '.' + mStrName[1], "");  //mFilePath = "D:\\NearWay_Oversea\\PubImage\\BuyInfo\\6\\"
                    if (!Directory.Exists(mFilePath))
                    {
                        Directory.CreateDirectory(mFilePath);
                    }
 
                    File.Copy(mPhysicalName, newFilePath, true);//小图:中文求购copy至海外求购
                    str = mPath + pImageNewName + '.' + mStrName[1];
                }
                System.Drawing.Image mNImage = mImage.GetThumbnailImage(mNWidth, mNHeith, null, IntPtr.Zero);
                mNImage.Save(Server.MapPath(str.Replace("_big_" + ViewState["mRandom"].ToString(), "")), mImage.RawFormat);
                string str2 = Server.MapPath(str.Replace("_big_" + ViewState["mRandom"].ToString(), ""));
                string str3 = str2.ToLower().Replace("nearway\\images", "NearWay_Oversea\\PubImage");
 
                File.Copy(str2, str3, true);//大图:中文求购copy至海外求购
            }
            else
            {
                myShare.WebMessageBox(this.Page, "图片格式不符合要求!");
                fldBuyInfoImage.Focus();
            }
        }
        else
        {
            myShare.WebMessageBox(this.Page, "请选择上传的文件!");
            fldBuyInfoImage.Focus();
        }
        return str;
    }

你可能感兴趣的:(copy)