日期:2014-05-18  浏览次数:20417 次

修改已经上传到数据库中的图片
我做了一个图片上传功能,数据库中保存的是图片所保存的虚拟路径,图片被保存在项目的某文件夹中,我想修改此图片,并把原有的图片替换掉(免得占内存),上传是能成的,但是我不知道如何修改,希望各位高手给一些例子,或一些想法,谢谢!

------解决方案--------------------
更新之前根据数据库的存放虚拟路径去对应的文件夹删除此图片后。
file.delete("../"+dt.rows[0]["imgPath"].ToString());
然后更新图片虚拟路径即可。
update ss set imgpath='"+新的图片路径+"' where a=id
------解决方案--------------------
那你需要可以操作服务器文件的接口,比如你做一个webservice,提交数据库时,顺便修改服务器具体文件,或者通过其他方式,比如ftp操作
------解决方案--------------------
并把原有的图片替换掉(免得占内存)?啥意思?
你存的是url,还替换什么?
------解决方案--------------------
那在你上传的时候,先根据数据库的路径删除旧的图片,然后再上传这些新的图片就是了。
------解决方案--------------------

参考以前我的- -
C# code
 #region 重新上传新资源
        private void FN_UpdateFile()
        {
            fileid = Request["fielid"];//获取文件编号
            folderid = Request["folderid"];//获取文件夹编号
            NRBLL.File bf = new Asiastar.NRBLL.File();
            Guid guid = new Guid(fileid);
            string createby = bf.FN_GetResListByFileid(guid).Tables[0].Rows[0]["CreateBy"].ToString();
            string createon = bf.FN_GetResListByFileid(guid).Tables[0].Rows[0]["CreateOn"].ToString();
            string modefyon = bf.FN_GetResListByFileid(guid).Tables[0].Rows[0]["ModefyOn"].ToString();
            string modefyBy = bf.FN_GetResListByFileid(guid).Tables[0].Rows[0]["ModefyBy"].ToString();
            if (bf.FN_GetResListByFileid(guid) != null || bf.FN_GetResListByFileid(guid).Tables[0].Rows.Count > 0)
            {
                string path = bf.FN_GetResListByFileid(guid).Tables[0].Rows[0]["FilePath"].ToString();//数据库中的文件完整路径
                if (path != null)   //如果这个文件存在
                {
                    System.IO.File.Delete(path);//先在服务器中删除
                }
                //遍历上传表单元素     开始重新上传
                HttpFileCollection files = HttpContext.Current.Request.Files;
                try
                {
                    for (int iFile = 0; iFile < files.Count; iFile++)
                    {
                        //检查文件扩展名字
                        HttpPostedFile postedFile = files[iFile];
                        string fileName = "";//定义文件名
                        //string fileExtension = "";
                        fileName = Path.GetFileName(postedFile.FileName);//得到上传文件的完整名称 即文件名+后缀名
                        int index = fileName.IndexOf(".");
                        string FileType = fileName.Substring(index).ToLower();//截取文件后缀名
                        Guid fileGuid = Guid.NewGuid();//生成新的文件名称 以GUID命名防止文件名相同
                        string NewFileName = fileGuid.ToString();//新的文件名
                        NewFileName = NewFileName + FileType;//新的文件名+后缀名
                        if (fileName != "")
                        {
                            try
                            {
                                string strpath = System.Web.HttpContext.Current.Server.MapPath("~/Upload/") + NewFileName;
                                NRModel.File model = new Asiastar.NRModel.File();
                                NRBLL.File nbf = new Asiastar.NRBLL.File();
                                Guid guid1 = new Guid(fileid);
                                Guid guid2 = new Guid(folderid);
                                Guid guid3 = new Guid(createby);
                                Guid guid4 = new Guid(modefyBy);
                                model.Fileid = guid1;
                                model.Folderid = guid2;
                                model.Filepath = strpath;
                                model.FileNam = fileName;
                                model.FileSize = postedFile.ContentLength;
                                model.Decription =