嗨,大家好,有人告诉我该代码有问题吗...我想更新图片,但此代码不会破坏我的工作,请帮助我... [英] Hi guys can any one tell me wats the problem with this code...i wanna update an image but this code not wrking plz help me...

查看:76
本文介绍了嗨,大家好,有人告诉我该代码有问题吗...我想更新图片,但此代码不会破坏我的工作,请帮助我...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if (Convert .ToByte (FileUpload2.PostedFile.ContentLength )> 0)
            {
               int imgsize = FileUpload2.PostedFile.ContentLength;

                int imagesize = imgsize / 1024;
                if (imagesize < 50)
                {
                    string fn = System.IO.Path.GetFileName(FileUpload2.PostedFile.FileName);
                    string[] fname1 = fn.Split(',');
                    int i = fname1.Length;
                    string fext = fname1[i - 1];
                    if (fext == "jpg")
                    {
                        string serverpath = Server.MapPath("./employeephoto/");
                        string filelocation = serverpath + "\\" + f[0] + "."+ fext;
                        //string filelocation = serverpath +"\\" + f[0] + ";."+ fext;  /** Path at server to save file**/
                        //string path = "./photo" + "\\"+ fn;   /** Path to enter in database **/
                        FileUpload2.PostedFile.SaveAs(filelocation);
                        /*Code to insert values into the table of spareparts*/



                        Label2.Text = "The Image has been uploaded successfully";
                    }

推荐答案

if(FileUpload2.HasFile)
{
int长度= FileUpload2.PostedFile.ContentLength;
byte [] imgbyte =新的byte [length];
如果(长度> 0)
{
//int imgsize = FileUpload2.PostedFile.ContentLength;

int imagesize =长度/1024;
如果(imagesize< 50)

if (FileUpload2.HasFile)
{
int length = FileUpload2.PostedFile.ContentLength;
byte[] imgbyte = new byte[length];
if (length> 0)
{
//int imgsize = FileUpload2.PostedFile.ContentLength;

int imagesize = length / 1024;
if (imagesize< 50)

{
string fn = System.IO.Path.GetFileName(FileUpload2.PostedFile.FileName);
string[] fname1 = fn.Split(',');
int i = fname1.Length;
string fext = fname1[i - 1];
if (fext == "jpg")
{
string serverpath = Server.MapPath("./employeephoto/");
string filelocation = serverpath + "\\" + f[0] + "."+ fext;
//string filelocation = serverpath +"\\" + f[0] + ";."+ fext; /** Path at server to save file**/
//string path = "./photo" + "\\"+ fn; /** Path to enter in database **/
FileUpload2.PostedFile.SaveAs(filelocation);
/*Code to insert values into the table of spareparts*/



Label2.Text = "The Image has been uploaded successfully";
}
  lblerror.Text = "Record updated successfully"


2件事:
1.确保您已上传新文件以进行上传.如果您认为较旧的一个路径文件仍然存在,那您就错了.因此,如果用户上传了新文件,则可以访问该文件进行上传.
2.确保在文件上传控件中检查"HasFile"属性.检查文件和NULL值很有帮助.

一旦有了文件,它将是简单的更新语句.如果用户未浏览和上传文件,则将无法对其进行更新.
2 things:
1. Make sure you have uploaded a new file for upload. If you think, older one path file still exists, you are mistaken. So, if user has uploaded a new file, you will have access to it for upload.
2. Make sure you check for ''HasFile'' property in fileupload control. Checking for file and NULL values are helpful.

Once you have the file, it would be simple update statement. If the file is not browsed and uploaded by user, you wont be able to update it.


if (FileUpload2.HasFile)
            {
                if (FileUpload2.FileName.Split(new char[] { '.' })[1].ToLower() == "jpg")
                {
                    FileUpload2.PostedFile.SaveAs(Server.MapPath(" ") + "\\employeephoto\\" + FileUpload2.FileName);
                }
            }


这篇关于嗨,大家好,有人告诉我该代码有问题吗...我想更新图片,但此代码不会破坏我的工作,请帮助我...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆