Javascript 服务器上的图像上载问题
请在以下问题上帮助我: 这是我的代码: ASPX代码:Javascript 服务器上的图像上载问题,javascript,jquery,asp.net,Javascript,Jquery,Asp.net,请在以下问题上帮助我: 这是我的代码: ASPX代码: <input type="Button" id="BtnUpload" class="button1" value="Upload Image" onclick="clickFileUpload();" /> ASPX页面上的Javascript函数: function UploadImage(path) //Added by Archana on 14-Aug-2013 { var Sketch_C
<input type="Button" id="BtnUpload" class="button1" value="Upload Image" onclick="clickFileUpload();" />
ASPX页面上的Javascript函数:
function UploadImage(path) //Added by Archana on 14-Aug-2013
{
var Sketch_Code,url,str;
Sketch_Code= document.getElementById('<%=txtCode.ClientID%>').value;
var strPage,strWidthProperty ;
var retVal="";
if(Sketch_Code !="")
{
if(path!="" && path!=null)
{
// var timeout = setTimeout("Imageresize(path,Sketch_Code);", 5000);
// alert(timeout);
var img = Imageresize(path,Sketch_Code);
if (document.getElementById('<%=imgload.ClientID%>').complete)
{alert("loaded");
url = "<%= ConfigurationManager.AppSettings("SiteURL").ToString() %>Upload_Images/800x800/" ;
document.getElementById('<%=imgload.ClientID%>').src =url + img;
document.getElementById('<%=imagePath.ClientID%>').value=img;
}
}
}
}
function clickFileUpload()
{
$('input[type=file]:first').trigger('click');
var imgpath=document.getElementById("imgfile").value;
//alert(imgpath);
if(imgpath =='')
{
// There is no file selected
alert("Please select valid file.");
}
else
{
var Extension = imgpath.substring(imgpath.lastIndexOf('.') + 1).toUpperCase();
if (Extension == "BMP" || Extension == "JPG" || Extension == "JPEG" || Extension == "GIF" || Extension == "PNG" )
{
UploadImage(imgpath);// Valid file type
}
else
{
// Not valid file type
alert("Please select valid extension file. (.bmp,.jpeg,.jpg,.png,.gif)");
}
}
}
function Imageresize(e,c)
{
//alert(e.id);
var Imagename = e;
var Code=c;
$.ajax({async : false,
type : 'GET', /*callback: function(index){CalculateTotal()},*/
url : "ImageResizer.aspx?Sketch_Code="+ c +"&imagepath=" + e,
success : function(response)
{
setTimeout('checkForAllImagesLoaded()', 5);
ImagesString = response;
// var ArrValues = ImagesString.split("~");
}
});
return ImagesString;
}
function UploadImage(path)//由Archana于2013年8月14日添加
{
变量代码、url、str;
草图代码=document.getElementById(“”).value;
var strPage,strWidthProperty;
var retVal=“”;
如果(草图代码!=“”)
{
如果(路径!=“”&路径!=null)
{
//var timeout=setTimeout(“图像大小调整(路径,草图代码);”,5000);
//警报(超时);
var img=图像调整大小(路径、草图代码);
if(document.getElementById(“”).complete)
{警报(“已加载”);
url=“上传图片/800x800/”;
document.getElementById(“”).src=url+img;
document.getElementById(“”).value=img;
}
}
}
}
函数clickFileUpload()
{
$('input[type=file]:first')。触发器('click');
var imgpath=document.getElementById(“imgfile”).value;
//警报(imgpath);
如果(imgpath=='')
{
//没有选择任何文件
警报(“请选择有效文件”);
}
其他的
{
var Extension=imgpath.substring(imgpath.lastIndexOf('.')+1.toUpperCase();
如果(扩展名==“BMP”| |扩展名==“JPG”| |扩展名==“JPEG”| |扩展名==“GIF”| |扩展名==“PNG”)
{
UploadImage(imgpath);//有效的文件类型
}
其他的
{
//无效的文件类型
警报(“请选择有效的扩展名文件。(.bmp、.jpeg、.jpg、.png、.gif)”);
}
}
}
函数Imageresize(e,c)
{
//警报(e.id);
var Imagename=e;
var代码=c;
$.ajax({async:false,
键入:'GET',/*回调:函数(索引){CalculateTotal()}*/
url:“ImageResizer.aspx?草图_代码=“+c+”&imagepath=“+e,
成功:功能(响应)
{
setTimeout('checkForAllImagesLoaded()',5);
ImagesString=响应;
//var ArrValues=ImagesString.split(“~”);
}
});
返回图像字符串;
}
这是我在ImageResizer页面上的代码:
Imports System.IO
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Drawing2D
Partial Class ImageResizer
Inherits System.Web.UI.Page
Public Shared Function ResizeImage(ByVal image As Image, ByVal path As String, ByVal size As Size, Optional ByVal preserveAspectRatio As Boolean = True) As Image
Dim newWidth As Integer
Dim newHeight As Integer
If preserveAspectRatio Then
Dim originalWidth As Integer = image.Width
Dim originalHeight As Integer = image.Height
Dim percentWidth As Single = CSng(size.Width) / CSng(originalWidth)
Dim percentHeight As Single = CSng(size.Height) / CSng(originalHeight)
Dim percent As Single
percent = IIf(percentHeight < percentWidth, percentHeight, percentWidth)
newWidth = CInt(originalWidth * percent)
newHeight = CInt(originalHeight * percent)
Else
newWidth = size.Width
newHeight = size.Height
End If
Dim newImage As Image = New Bitmap(newWidth, newHeight)
Using graphicsHandle As Graphics = Graphics.FromImage(newImage)
graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic
graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight)
End Using
newImage.Save(path)
Return newImage
End Function
Function UploadFileToServer(ByVal filepath As String, ByVal code As String) As String
Dim scriptStr As String = ""
Dim FileName As String
Try
Dim fileExtension As String = Path.GetExtension(filepath).ToLower()
FileName = code.Trim() + DateTime.Today.ToShortDateString().Replace("/", "") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + fileExtension
File.Copy(filepath, Server.MapPath("Upload_Images/") & FileName)
Catch ex As Exception
End Try
Return FileName
End Function
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim filepath, resizedpath, code As String
filepath = Request.QueryString("imagepath")
code = Request.QueryString("Sketch_Code")
If filepath <> "" Then
filepath = UploadFileToServer(filepath, code)
Dim original As Image = Image.FromFile(Server.MapPath("Upload_Images/") & filepath)
resizedpath = Server.MapPath("Upload_Images/200X200/") & filepath
ResizeImage(original, resizedpath, New Size(200, 200))
resizedpath = Server.MapPath("Upload_Images/800X800/") & filepath
ResizeImage(original, resizedpath, New Size(800, 800))
Response.Expires = -1
Response.ContentType = "text/plain"
Response.Write(filepath.ToString())
Response.End()
End If
End Sub
End Class
Imports System.IO
导入系统。绘图
导入System.Drawing.Imaging
导入System.Drawing.Drawing2D
部分类图像大小调整器
继承System.Web.UI.Page
公共共享函数ResizeImage(ByVal image作为图像,ByVal path作为字符串,ByVal size作为大小,可选的ByVal preserveSpectratio作为Boolean=True)作为图像
将newWidth设置为整数
将newHeight设置为整数
如果保留Aspectratio,则
Dim originalWidth为整数=image.Width
Dim originalHeight为整数=图像高度
单个尺寸百分比宽度=CSng(尺寸宽度)/CSng(原始宽度)
单个尺寸百分比高度=CSng(尺寸高度)/CSng(原始高度)
单程票的微弱百分比
百分比=IIf(百分比高度<百分比宽度,百分比高度,百分比宽度)
newWidth=CInt(原始宽度*百分比)
新高度=CInt(原始高度*百分比)
其他的
newWidth=大小。宽度
newHeight=size.Height
如果结束
将新图像变暗为图像=新位图(新宽度、新高度)
使用graphicsHandle作为Graphics=Graphics.FromImage(newImage)
graphicsHandle.InterpolationMode=InterpolationMode.HighQualityBicubic
graphicsHandle.DrawImage(图像,0,0,新宽度,新高度)
终端使用
newImage.Save(路径)
返回新图像
端函数
函数UploadFileToServer(ByVal filepath作为字符串,ByVal代码作为字符串)作为字符串
Dim scriptStr As String=“”
将文件名设置为字符串
尝试
Dim fileExtension As String=Path.GetExtension(filepath.ToLower())
FileName=code.Trim()+DateTime.Today.toSortDateString().Replace(“/”,“)+DateTime.Now.Hour.ToString()+DateTime.Now.Minute.ToString()+DateTime.Now.Second.ToString()+文件扩展名
File.Copy(filepath、Server.MapPath(“Upload_Images/”)和文件名)
特例
结束尝试
返回文件名
端函数
受保护的子页加载(ByVal sender作为对象,ByVal e作为System.EventArgs)处理Me.Load
Dim文件路径,大小调整路径,代码为字符串
filepath=Request.QueryString(“imagepath”)
代码=请求.查询字符串(“草图代码”)
如果文件路径为“”,则
filepath=UploadFileToServer(文件路径,代码)
Dim original As Image=Image.FromFile(Server.MapPath(“Upload_Images/”)&filepath)
resizedpath=Server.MapPath(“Upload_Images/200X200/”)&filepath
调整图像大小(原始、调整路径大小、新大小(200200))
resizedpath=Server.MapPath(“Upload_Images/800X800/”)&filepath
调整图像大小(原始、调整路径大小、新大小(800800))
Response.Expires=-1
Response.ContentType=“text/plain”
Response.Write(filepath.ToString())
答复:End()
如果结束
端接头
末级
此代码在我的本地(开发环境)上运行良好
但当我在服务器上部署了$.ajax函数后尝试上载时,我的代码无法用于$.ajax函数。无法获取返回我的imagestring的响应
请帮助它返回您,GDI错误 有关GDI错误,请参阅: 这是因为“Upload_Images/”目录丢失 所以要找到合适的位置 您必须在Catch块中编写以下代码才能打印
Response.Write(Server.MapPath("Upload_Images/"));
这将返回上载图像文件夹的正确路径