控件:pictureBox, button要求:将图片上传到服务器的文件夹里,上传时,若图片的同名,同路径,用新的图片替换旧的图片,即把旧的图片从服务器的文件夹里删除掉,只保存新图片。请高手帮忙修改以下上传的方法,给一个思路!上传方法:
/// ftp服务器IP
        /// </summary>
        string ftpServerIP;
        /// <summary>
        /// ftp用户名
        /// </summary>
        string ftpUserID;
        /// <summary>
        /// ftp密码
        /// </summary>
        string ftpPassword;
FtpWebRequest reqFTP;
 /// </summary>
        /// <param name="Filename">上传文件名称</param>
        /// <param name="Dir">上传文件夹</param>
        /// <returns></returns>
        public bool Upload(string Filename,string Dir,string SaveName)
        {
            FileInfo fileinf = new FileInfo(Filename);
            string uri = "ftp://" + ftpServerIP + "/" + Dir + "/" + SaveName;            Connect(uri);            reqFTP.KeepAlive = false;
            //指定执行什么指令
            reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
            //指示服务器上传文件大小
            reqFTP.ContentLength = fileinf.Length;
            //设置缓冲区大小
            int buffLength = 2048;            byte[] buff = new byte[buffLength];
            int contentLen;
            //打开一个文件流来读取上传文件
            FileStream fs = fileinf.OpenRead();
            try
            {
                //上传文件写入流
                Stream strm = reqFTP.GetRequestStream();
                contentLen = fs.Read(buff, 0, buffLength);
                while (contentLen != 0)
                {
                    strm.Write(buff, 0, contentLen);
                    contentLen = fs.Read(buff, 0, buffLength);
                }
                strm.Close();
                fs.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
                throw new Exception(ex.Message);
            }
        }

解决方案 »

  1.   

    在图片名称前加时间
    DateTime.Now.ToString("yyyyMMddHHmmss") + Filename
      

  2.   

    webservice 怎么用呢?请赐教
      

  3.   


    //图片转成二进制
        public static byte[] getBinaryFile(string filename)
            {
                byte[] bt = null;
                if (File.Exists(filename))
                {
                    try
                    {
                        ///打开现有文件以进行读取。
                        FileStream s = File.OpenRead(filename);
                        bt = ConvertStreamToByteBuffer(s);
                        s.Close();
                    }
                    catch (Exception e)
                    {
                        return new byte[0];
                    }
                }
                else
                {
                    return new byte[0];
                }
                return bt;
            }
    //上传到服务器(写在WebService里)
      public bool Upload(byte[] fs, string FileName)
        {
            //   string FileName = System.DateTime.Now.ToString("yyyyMMddHHmmssms") + "." + fileType;
            try
            {
                ///定义并实例化一个内存流,以存放提交上来的字节数组。
                MemoryStream m = new MemoryStream(fs);
                ///定义实际文件对象,保存上载的文件。
                FileStream f = new FileStream(Server.MapPath(".") + "\\Image\\"
                 + FileName, FileMode.Create);
                ///把内内存里的数据写入物理文件
                m.WriteTo(f);
                m.Close();
                f.Close();
                f = null;
                m = null;
                return true;
            }
            catch (Exception ex)
            {
                return false; ;
            }
        } //调用
    byte[] _ImageByte = Common.getBinaryFile(pictureBox1.ImageLocation);
    Common.CuurentCommon.CurrentService.Upload(_ImageByte, txt_filepath.Text.Trim());