如何更新存储在文件夹中的图像

时间:2017-08-12 21:23:51

标签: c# asp.net file-upload

我有一个网格视图,显示包含更新和删除选项的项目表。我的问题是更新项目图像。当我更新项目和UploadPhoto.HasFile=false时,这意味着我没有选择图像。在else语句中,我想将项目图像作为默认值或获取项目图像。这是我的代码:

 int projectid = Convert.ToInt32(e.CommandArgument);

  if (UploadPhoto.HasFile)
                {

                    try
                    {
                        if (UploadPhoto.PostedFile.ContentType == "image/jpeg" ||
                            UploadPhoto.PostedFile.ContentType == "image/.jpg" ||
                            UploadPhoto.PostedFile.ContentType == "image/.psd" ||
                            UploadPhoto.PostedFile.ContentType == "image/.png" ||
                            UploadPhoto.PostedFile.ContentType == "image/.gif")
                        {
                            if (UploadPhoto.PostedFile.ContentLength < 8729744)
                            {
                                string filename = Path.GetFileName(UploadPhoto.FileName);
                                UploadPhoto.SaveAs(Server.MapPath
                                    ("~/ProjectsImages/" + 
                                      projectid + filename));

                            }

                        }
                    }
                    catch (Exception ex)
                    {

                    }
                }
                else
                {

                    string relativePath = "/ProjectsImages/";
                    var path = Server.MapPath(relativePath);
                    string[] filePaths = Directory.GetFiles(path, projectid + "*");
                    UploadPhoto.FileName = filePaths; //here what i want to do 

                }
// At this line I update the project and UploadPhoto.FileName = null
// This overrides the project image 

                context.Update_Project_Detail(txtProjectName.Text, 
                    txtAreas.Text, 
                    DateTime.Parse(txtEditDate.Text),
                    txtClassification.Value, 
                    UploadPhoto.FileName, projectid);

我在else语句中想要的是获取projectimage名称并将其分配给UploadPhoto.filename

0 个答案:

没有答案