ASPX ajax表格后帮助

时间:2010-04-17 05:41:03

标签: asp.net ajax asp.net-ajax vbscript

嘿所有,我有这个代码,允许用户选择jpg图像,调整大小并将其上传到服务器driectory。问题在于它在保存图像时重新加载aspx页面。

我的问题是 - 是否有任何方法可以做同样的事情但是使用ajax以便它在提交后不会离开页面?我用经典的asp页面做了很多次,但从未使用过aspx页面。

以下是ASPX页面的代码:

<%@ Page Trace="False" Language="vb" aspcompat="false" debug="true" validateRequest="false"%> 
<%@ Import Namespace=System.Drawing %>
<%@ Import Namespace=System.Drawing.Imaging %>
<%@ Import Namespace=System.Drawing.Text %>
<%@ Import Namespace=System %>
<%@ Import Namespace=System.IO %>
<%@ Import Namespace=System.Web %>
<%@ Import Namespace=System.ServiceProcess %>
<%@ Import Namespace=Microsoft.Data.Odbc %>
<%@ Import Namespace=System.Data.Odbc %>
<%@ Import Namespace=MySql.Data.MySqlClient %>
<%@ Import Namespace=MySql.Data %>
<%@ Import Namespace=System.Drawing.Drawing2D %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.ADO" %>

<%@ Import Namespace=ADODB %>
<SCRIPT LANGUAGE="VBScript" runat="server">
const Lx = 200
const Ly = 60
const upload_dir = "/img/avatar/"
const upload_original = "tmpAvatar"
const upload_thumb = "thumb"
const upload_max_size = 256
dim fileExt
dim newWidth, newHeight as integer
dim l2
dim fileFld as HTTPPostedFile
Dim originalimg As System.Drawing.Image
dim msg
dim upload_ok as boolean
</script>
<%
Dim theID, theEmail, maleOrFemale

theID = Request.QueryString("ID")
theEmail = Request.QueryString("eMail")
maleOrFemale = Request.QueryString("MF")

randomize()
upload_ok = false

if lcase(Request.ServerVariables("REQUEST_METHOD"))="post" then
    fileFld = request.files(0)
    if fileFld.ContentLength > upload_max_size * 1024 then
        msg = "Sorry, the image must be less than " & upload_max_size & "Kb"
    else
        try
            fileExt = System.IO.Path.GetExtension(fileFld.FileName).ToLower()

            if fileExt = ".jpg" then
                originalImg = System.Drawing.Image.FromStream(fileFld.InputStream)              

                if originalImg.Height > Ly then
                    newWidth = Ly * (originalImg.Width / originalImg.Height)
                    newHeight = Ly
                end if

                Dim thumb As New Bitmap(newWidth, newHeight)
                Dim gr_dest As Graphics = Graphics.FromImage(thumb)
                dim sb = new SolidBrush(System.Drawing.Color.White)

                gr_dest.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
                gr_dest.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality
                gr_dest.FillRectangle(sb, 0, 0, thumb.Width, thumb.Height)
                gr_dest.DrawImage(originalImg, 0, 0, thumb.Width, thumb.Height)

                try     
                    originalImg.save(Server.MapPath(upload_dir & upload_original & fileExt), originalImg.rawformat)
                    thumb.save(Server.MapPath(upload_dir & theID & fileExt), originalImg.rawformat)
                    msg = "Uploaded " & fileFld.FileName & " to " & Server.MapPath(upload_dir & upload_original & fileExt)
                    upload_ok = true
                    File.Delete(Server.MapPath(upload_dir & upload_original & fileExt))
                catch
                    msg = "Sorry, there was a problem saving your avatar. Please try again."
                end try

                if not thumb is nothing then
                    thumb.Dispose()
                    thumb = nothing
                end if
            else
                msg = "That image does not seem to be a JPG. Upload only JPG images."
            end if
        catch
            msg = "That image does not seem to be a JPG."
        end try
    end if

    if not originalImg is nothing then
        originalImg.Dispose()
        originalImg = nothing
    end if  
end if
%><head>
<meta http-equiv="pragma" content="no-cache" />
</head>
<html>
<script type="text/javascript" src="js/jquery-1.3.min.js"></script>

<form enctype="multipart/form-data" method="post" runat="server" id="sendImg">
<input type="file" name="upload_file" id="upload_file" style="-moz-opacity: 0; opacity:0; filter: alpha(opacity=0); margin-top: 5px; float:left; cursor:pointer;" onChange="$('#sendImg').submit();" >
<input type="submit" value="Upload" style="visibility:hidden; display:none;">
</form>
</body>
</html>

任何帮助都会很棒! :O)

大卫

1 个答案:

答案 0 :(得分:0)

ajax和&lt; input type="file">不能很好地协同工作。基础XMLHttpRequest对象受到限制,无法实际访问文件上载。您可以使用任何与文件上传相关的javascript进行非常有限的一组操作。假冒你想要做的事情的最简单方法是使用iframe。