请教:freetextbox1.63的上传图片不成功的问题。附代码
我用的是修改过的代码:
原来的上传图片功能是可以管理如upload目录里所有图片的功能
我现在修改后是只能上传图片,而不能看见upload目录里的其它图片
此功能在本地运行成功,但是程序上传到服务器上时,图片依然上传成功,根目录下upload目录里有上传的图片,但是freetextbox里显示出错误的图片,检查图片路径后发现路径为:http://upload/0410210903_253.gif
显然前面的:www.xxx.com掉了。。
请大家帮我看看下面的代码哪里有错误,导致了上面的问题。。
<%@ 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","JPG","JPEG","JPE","GIF"};
// Configuration
private bool UploadIsEnabled = true; // 是否允许上传文件
private bool DeleteIsEnabled = false; // 是否允许删除文件
private string DefaultImageFolder = "upload"; // 默认的起始文件夹
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"];
RootImagesFolder.Value = Server.MapPath(".");
CurrentImagesFolder.Value = DefaultImageFolder;
UploadPanel.Visible = UploadIsEnabled;
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) {
}
} 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 = DateTime.Now.ToString("yyMMddhhmm")+"_"+UploadFileName.Substring(UploadFileName.LastIndexOf("\\")+1);
UploadFileDestination = Request.PhysicalApplicationPath;
UploadFileDestination += CurrentImagesFolder.Value;
UploadFileDestination += "\\";
UploadFile.PostedFile.SaveAs(UploadFileDestination + UploadFileName);
ResultsMessage.Text = UploadSuccessMessage;
DisplayImages(UploadFileName);
Image1.Visible = true;
} 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;
}
}
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 = 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 = 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 FileName) {
string filelocation = HttpContext.Current.Request.PhysicalApplicationPath+DefaultImageFolder+"\\"+FileName;
string filevis = HttpContext.Current.Request.ApplicationPath+DefaultImageFolder+"\\"+FileName;
System.Drawing.Image myImage = System.Drawing.Image.FromFile(filelocation);
int Width = myImage.Width;
int Height = myImage.Height;
if(Width > 520) Width=520;
if(Height > 400) Height=400;
myImage.Dispose();
if(System.IO.File.Exists(filelocation))
{
Image1.ImageUrl =Request.ApplicationPath+"/" + DefaultImageFolder.Replace("\\","/")+"/"+ FileName;
Image1.Width = Width; Image1.Height = Height;
Image1.Attributes["ondblclick"]="returnImage('"+ Request.ApplicationPath+"/" + DefaultImageFolder.Replace("\\","/")+"/"+ FileName + "' ,'"+Width+"','"+Height+"');";
}
}
</script>
<asp:panel 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>