主题:  图片上传的问题,搞了两天没看出什么问题

cloudroot

职务:普通成员
等级:1
金币:0.0
发贴:21
#12005/7/26 11:51:34
<%@ Page language="c#" %>
<script runat="server">

// Messages
private string NoFileMessage = "您没有选择文件。";
private string UploadSuccessMessage = "上传成功";
private string UploadFailureMessage = "上传失败。";
private string NoImagesMessage = "该文件夹不存在或者是空的";
private string NoFolderSpecifiedMessage = "您要上传到的文件夹不存在。";
private string NoFileToDeleteMessage = "您没有选中要删除的文件。";
private string InvalidFileTypeMessage = "您无法上传这种类型的文件。";
private string[] AcceptedFileTypes = new string[] {"jpg","jpeg","jpe","gif","png"};

// Configuration
private bool    UploadIsEnabled = true; // 是否允许上传文件
private bool    DeleteIsEnabled = true; // 是否允许删除文件
private string    DefaultImageFolder = "images"; // 默认的起始文件夹

private void Page_Load(object sender, System.EventArgs e) {
    string isframe = "" + Request["frame"];
    
    if (isframe != "" {
        MainPage.Visible = true;
        iframePanel.Visible = false;
    
        string rif = "" + Request["rif"];
        string cif = "" + Request["cif"];    

        if (cif != "" && rif != "" {
            RootImagesFolder.value = rif;
            CurrentImagesFolder.value = cif;
        } else {
            RootImagesFolder.value = DefaultImageFolder;
            CurrentImagesFolder.value = DefaultImageFolder;    
        }

        UploadPanel.Visible = UploadIsEnabled;
        DeleteImage.Visible = DeleteIsEnabled;

        string FileErrorMessage = "";
        string ValidationString = ".*(";
        //[\.jpg]|[\.jpeg]|[\.jpe]|[\.gif]|[\.png])$"
        for (int i=0;i<AcceptedFileTypes.Length; i++) {
            ValidationString += "[\\." + AcceptedFileTypes[i] + "]";
            if (i < (AcceptedFileTypes.Length-1)) ValidationString += "|";
            FileErrorMessage += AcceptedFileTypes[i];
            if (i < (AcceptedFileTypes.Length-1)) FileErrorMessage += ", ";
        }
        FileValidator.ValidationExpression = ValidationString+"$";
        FileValidator.ErrorMessage=FileErrorMessage;

        if (!IsPostBack) {
            DisplayImages();
        }
    } else {
        
    }
}

public void UploadImage_onClick(object sender, EventArgs e) {    
    if (Page.IsValid) {
        if (CurrentImagesFolder.value != "" {
            if (UploadFile.PostedFile.FileName.Trim() != "" {
                if (IsValidFileType(UploadFile.PostedFile.FileName)) {
                    try {
                        string UploadFileName = "";
                        string UploadFileDestination = "";
                        UploadFileName = UploadFile.PostedFile.FileName;
                        UploadFileName = UploadFileName.Substring(UploadFileName.LastIndexOf("\\"+1);
                        UploadFileDestination = HttpContext.Current.Request.PhysicalApplicationPath;
                        UploadFileDestination += CurrentImagesFolder.value;
                        UploadFileDestination += "\\";
                        UploadFile.PostedFile.SaveAs(UploadFileDestination + UploadFileName);
                        ResultsMessage.Text = UploadSuccessMessage;
                    } catch(Exception ex) {
                        //ResultsMessage.Text = "Your file could not be uploaded: " + ex.Message;
                        ResultsMessage.Text = UploadFailureMessage;
                    }
                } else {
                    ResultsMessage.Text = InvalidFileTypeMessage;
                }
            } else {
                ResultsMessage.Text = NoFileMessage;
            }
        } else {
            ResultsMessage.Text = NoFolderSpecifiedMessage;
        }
    } else {
        ResultsMessage.Text = InvalidFileTypeMessage;
        
    }
    DisplayImages();
}

public void DeleteImage_onClick(object sender, EventArgs e) {
    if (FileToDelete.value != "" && FileToDelete.value != "undefined" {
        try {
            string AppPath = HttpContext.Current.Request.PhysicalApplicationPath;
            System.IO.File.Delete(AppPath + CurrentImagesFolder.value + "\\" + FileToDelete.value);
            ResultsMessage.Text = "已删除: " + FileToDelete.value;
        } catch(Exception ex) {            
            ResultsMessage.Text = "删除失败。";
        }
    } else {
        ResultsMessage.Text = NoFileToDeleteMessage;
    }
    DisplayImages();
}

private bool IsValidFileType(string FileName) {
    string ext = FileName.Substring(FileName.LastIndexOf("."+1,FileName.Length-FileName.LastIndexOf("."-1);
    for (int i=0; i<AcceptedFileTypes.Length; i++) {
        if (ext == AcceptedFileTypes[i]) {
            return true;
            
        }    
    }
    return false;
}


private string[] ReturnFilesArray() {
    if (CurrentImagesFolder.value != "" {
        try {
            string AppPath = HttpContext.Current.Request.PhysicalApplicationPath;
            string ImageFolderPath = AppPath + CurrentImagesFolder.value;
            string[] FilesArray = System.IO.Directory.GetFiles(ImageFolderPath,"*";
            return FilesArray;
            
            
        } catch {
        
            return null;
        }
    } else {
        return null;
    }

}

private string[] ReturnDirectoriesArray() {
    if (CurrentImagesFolder.value != "" {
        try {
            string AppPath = HttpContext.Current.Request.PhysicalApplicationPath;
            string CurrentFolderPath = AppPath + CurrentImagesFolder.value;
            string[] DirectoriesArray = System.IO.Directory.GetDirectories(CurrentFolderPath,"*";
            return DirectoriesArray ;
        } catch {
            return null;
        }
    } else {
        return null;
    }
}

public void DisplayImages() {
    string[] FilesArray = Return FilesArray();
    string[] DirectoriesArray = ReturnDirectoriesArray();
    string AppPath = HttpContext.Current.Request.PhysicalApplicationPath;
    string AppUrl;
    
    //Get the application's URL
    if (Request.ApplicationPath == "/"
        AppUrl = Request.ApplicationPath;
    else
        AppUrl = Request.ApplicationPath + "/";
    
    GalleryPanel.Controls.Clear();
    if ( (FilesArray == null || FilesArray.Length == 0) && (DirectoriesArray == null || DirectoriesArray.Length == 0) ) {
        gallerymessage.Text = NoImagesMessage + ": " + RootImagesFolder.value;
    } else {
        string ImageFileName = "";
        string ImageFileLocation = "";

        int thumbWidth = 94;
        int thumbHeight = 94;
        
        if (CurrentImagesFolder.value != RootImagesFolder.value) {

            System.Web.UI.HtmlControls.HtmlImage myHtmlImage = new System.Web.UI.HtmlControls.HtmlImage();
            myHtmlImage.Src = AppUrl + "images/ftb/folder.up.gif";
            myHtmlImage.Attributes["unselectable"]="on";
            myHtmlImage.Attributes["align"]="absmiddle";
            myHtmlImage.Attributes["vspace"]="36";

            string ParentFolder = CurrentImagesFolder.value.Substring(0,CurrentImagesFolder.value.LastIndexOf("\\");

            System.Web.UI.WebControls.Panel myImageHolder = new System.Web.UI.WebControls.Panel();                    
            myImageHolder.CssClass = "imageholder";
            myImageHolder.Attributes["unselectable"]="on";
            myImageHolder.Attributes["onclick"]="divClick(this,'');";
            myImageHolder.Attributes["ondblclick"]="gotoFolder('" + RootImagesFolder.value + "','" + ParentFolder.Replace("\\","\\\\" + "');";
            myImageHolder.Controls.Add(myHtmlImage);

            System.Web.UI.WebControls.Panel myMainHolder = new System.Web.UI.WebControls.Panel();
            myMainHolder.CssClass = "imagespacer";
            myMainHolder.Controls.Add(myImageHolder);

            System.Web.UI.WebControls.Panel myTitleHolder = new System.Web.UI.WebControls.Panel();
            myTitleHolder.CssClass = "titleHolder";
            myTitleHolder.Controls.Add(new LiteralControl("向上");
            myMainHolder.Controls.Add(myTitleHolder);

            GalleryPanel.Controls.Add(myMainHolder);        
            
        }
        
        foreach (string _Directory in DirectoriesArray) {
            
            try {
                string DirectoryName = _Directory.ToString();
                

                System.Web.UI.HtmlControls.HtmlImage myHtmlImage = new System.Web.UI.HtmlControls.HtmlImage();
                myHtmlImage.Src = AppUrl + "images/ftb/folder.big.gif";
                myHtmlImage.Attributes["unselectable"]="on";
                myHtmlImage.Attributes["align"]="absmiddle";
                myHtmlImage.Attributes["vspace"]="29";

                System.Web.UI.WebControls.Panel myImageHolder = new System.Web.UI.WebControls.Panel();                    
                myImageHolder.CssClass = "imageholder";
                myImageHolder.Attributes["unselectable"]="on";
                myImageHolder.Attributes["onclick"]="divClick(this);";
                myImageHolder.Attributes["ondblclick"]="gotoFolder('" + RootImagesFolder.value + "','" + DirectoryName.Replace(AppPath,"".Replace("\\","\\\\" + "');";
                myImageHolder.Controls.Add(myHtmlImage);

                System.Web.UI.WebControls.Panel myMainHolder = new System.Web.UI.WebControls.Panel();
                myMainHolder.CssClass = "imagespacer";
                myMainHolder.Controls.Add(myImageHolder);

                System.Web.UI.WebControls.Panel myTitleHolder = new System.Web.UI.WebControls.Panel();
                myTitleHolder.CssClass = "titleHolder";
                myTitleHolder.Controls.Add(new LiteralControl(DirectoryName.Replace(AppPath + CurrentImagesFolder.value + "\\",""));
                myMainHolder.Controls.Add(myTitleHolder);

                GalleryPanel.Controls.Add(myMainHolder);        
            } catch {
                // nothing for error
            }
        }
        
        foreach (string ImageFile in FilesArray) {

            try {

                ImageFileName = ImageFile.ToString();
                ImageFileName = ImageFileName.Substring(ImageFileName.LastIndexOf("\\"+1);
                ImageFileLocation = AppUrl;
                ImageFileLocation = ImageFileLocation.Substring(ImageFileLocation.LastIndexOf("\\"+1);
                //galleryfilelocation += "/";
                ImageFileLocation += CurrentImagesFolder.value;
                ImageFileLocation += "/";
                ImageFileLocation += ImageFileName;
                System.Web.UI.HtmlControls.HtmlImage myHtmlImage = new System.Web.UI.HtmlControls.HtmlImage();
                myHtmlImage.Src = ImageFileLocation;
                System.Drawing.Image myImage = System.Drawing.Image.FromFile(ImageFile.ToString());
                myHtmlImage.Attributes["unselectable"]="on";
                //myHtmlImage.border=0;

                // landscape image
                if (myImage.Width > myImage.Height) {
                    if (myImage.Width > thumbWidth) {
                        myHtmlImage.Width = thumbWidth;
                        myHtmlImage.Height = Convert.ToInt32(myImage.Height * thumbWidth/myImage.Width);                        
                    } else {
                        myHtmlImage.Width = myImage.Width;
                        myHtmlImage.Height = myImage.Height;
                    }
                // portrait image
                } else {
                    if (myImage.Height > thumbHeight) {
                        myHtmlImage.Height = thumbHeight;
                        myHtmlImage.Width = Convert.ToInt32(myImage.Width * thumbHeight/myImage.Height);
                    } else {
                        myHtmlImage.Width = myImage.Width;
                        myHtmlImage.Height = myImage.Height;
                    }
                }
                
                if (myHtmlImage.Height < thumbHeight) {
                    myHtmlImage.Attributes["vspace"] = Convert.ToInt32((thumbHeight/2)-(myHtmlImage.Height/2)).ToString();
                }


                System.Web.UI.WebControls.Panel myImageHolder = new System.Web.UI.WebControls.Panel();                    
                myImageHolder.CssClass = "imageholder";
                myImageHolder.Attributes["onclick"]="divClick(this,'" + ImageFileName + "');";
                myImageHolder.Attributes["ondblclick"]="returnImage('" + ImageFileLocation.Replace("\\","/" + "','" + myImage.Width.ToString() + "','" + myImage.Height.ToString() + "');";
                myImageHolder.Controls.Add(myHtmlImage);


                System.Web.UI.WebControls.Panel myMainHolder = new System.Web.UI.WebControls.Panel();
                myMainHolder.CssClass = "imagespacer";
                myMainHolder.Controls.Add(myImageHolder);

                System.Web.UI.WebControls.Panel myTitleHolder = new System.Web.UI.WebControls.Panel();
                myTitleHolder.CssClass = "titleHolder";
                myTitleHolder.Controls.Add(new LiteralControl(ImageFileName + "<BR>" + myImage.Width.ToString() + "x" + myImage.Height.ToString()));
                myMainHolder.Controls.Add(myTitleHolder);

                //GalleryPanel.Controls.Add(myImage);
                GalleryPanel.Controls.Add(myMainHolder);
                
                myImage.Dispose();
            } catch {

            }
        }
        gallerymessage.Text = "";
    }
}
</script>
<aspanel id="MainPage" runat="server" visible="false">
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<META HTTP-EQUIV="Expires" CONTENT="0">
<title>插入图片</title>
<style>

body {
    margin: 0px 0px 0px 0px;
    padding: 0px 0px 0px 0px;
    background: #ffffff;
    width: 100%;
    overflow:hidden;
    border: 0;
}

body,tr,td {
    color: #000000;
    font-family: Verdana, Arial, Helvetica, sans-serif;
    font-size: 10pt;
}

div.imagespacer {
    width: 120;
    height: 126;
    text-align: center;            
    float: left;
    font: 10pt verdana;
    margin: 5px;
    overflow: hidden;
}
div.imageholder {
    margin: 0px;
    padding: 0px;
    border: 1 solid #CCCCCC;
    width: 100;
    height: 100;
}

div.titleholder {
    font-family: ms sans serif, arial;
    font-size: 8pt;
    width: 100;
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;            
}        

</style>


<script language="javascript">
lastDiv = null;
function divClick(theDiv,filename) {
    if (lastDiv) {
        lastDiv.style.border = "1 solid #CCCCCC";
    }
    lastDiv = theDiv;
    theDiv.style.border = "2 solid #316AC5";
    
    document.getElementById("FileToDelete".value = filename;

}
function gotoFolder(rootfolder,newfolder) {
    window.navigate("ftb.imagegallery.aspx?frame=1&rif=" + rootfolder + "&cif=" + newfolder);
}        
function returnImage(imagename,width,height) {
    var arr = new Array();
    arr["filename"] = imagename;
    arr["width"] = width;
    arr["height"] = height;            
    window.parent.returnvalue = arr;
    window.parent.close();    
}        
</script>        
</HEAD>
<body>
<table width=100% height=100% cellpadding=0 cellspacing=0 border=0>

<FORM encType="multipart/form-data" runat="server">

<tr><td>
    <div id="galleryarea" style="width=100%; height:100%; overflow: auto;">
        <asp:label id="gallerymessage" runat="server"></asp:label>
        <aspanel id="GalleryPanel" runat="server"></aspanel>
    </div>
</td></tr>
<aspanel id="UploadPanel" runat="server">
<tr><td height=16 style="padding-left:10px;border-top: 1 solid #999999; background-color:#99ccff;">
    
    <table>
    <tr>
        <td valign=top><input id="UploadFile" type="file" name="UploadFile" runat="server" style="width:300;"/></td>
        <td valign=top><asp:button id="UploadImage" Text="上传" runat="server" onclick="UploadImage_onClick" /></td>
        <td valign=top><asp:button id="DeleteImage" Text="删除" runat="server" onclick="DeleteImage_onClick" /></td>
        <td valign=middle>        
    </tr>
    <tr>
        <td colspan=3>
            <asp:RegularExpressionValidator runat="server"
                ControlToValidate="UploadFile"
                id="FileValidator" display="dynamic"/>
            <asp:literal id="ResultsMessage" runat="server" />        
        </td>        
    </tr></table>    
    <input type="hidden" id="FileToDelete" value="" runat="server" />
    <input type="hidden" id="RootImagesFolder" value="images" runat="server" />
    <input type="hidden" id="CurrentImagesFolder" value="images" runat="server" />
</td></tr>
</aspanel>
</form>
</table>
</body>
</HTML>
</aspanel>
<aspanel id="iframePanel" runat="server" >
<html>
<head><title>插入图片</title></head>
<style>
body {
    margin: 0px 0px 0px 0px;
    padding: 0px 0px 0px 0px;
    background: #ffffff;
    overflow:hidden;
}
</style>
<body>
    <iframe style="width:100%;height:100%;border:0;" border=0 frameborder=0 src="ftb.imagegallery.aspx?frame=1&<%=Request.QueryString%>"></iframe>
</body>
</html>
</aspanel>



cloudroot

职务:普通成员
等级:1
金币:0.0
发贴:21
#22005/7/26 11:52:10
这是别人写的图片上传程序,有点乱,

而且是有错误的,上传不了图片,提示是"该文件夹不存在或者是空的"

点击上传没有一点反映



cloudroot

职务:普通成员
等级:1
金币:0.0
发贴:21
#32005/7/26 11:52:56
各为帮帮忙,公司催着要



蓝鲸

职务:版主
等级:5
金币:42.1
发贴:2614
#42005/7/26 23:44:31
还不如自己看点书,其实上传图片的代码不复杂质的,这么大段的上来没锁住你是便宜了。
一点要了解代码,其它一些都是验证等代码, 一大堆的。


非常大鱼