先到官方網站去抓檔案下來
我所使用的版本是v2.2.0.1
SWFUpload下載網址:http://swfupload.googlecode.com
SWFUpload下載檔案:SWFUpload v2.2.0.1 Samples.zip
然後將下載下來的檔案解壓縮後,複製裡面的「applicationdemo.vb.net」
路徑為:\SWFUpload v2.2.0.1 Samples\SWFUpload v2.2.0.1 Samples\demos
檔案結構如圖:(圖的來源為 F6 Team)
大致上需要改三個檔案:[default.aspx] [default.aspx.vb] [upload.aspx.vb]
-------------------------
Default.aspx的內容:
<%@ Page Language="VB" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default"%> SWFUpload Revision v2.1.0 Application Demo (ASP.Net VB.Net 2.0)
----------------------------------
Default.aspx.vb的內容:
Partial Class _Default Inherits System.Web.UI.Page Protected AuthCookie As String Public Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs) Session.Clear() Dim auth_cookie As HttpCookie = Request.Cookies(FormsAuthentication.FormsCookieName) If Not auth_cookie Is Nothing Then AuthCookie = auth_cookie.Value End If End Sub Protected Sub btnLogout_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLogout.Click FormsAuthentication.SignOut() FormsAuthentication.RedirectToLoginPage() End Sub End Class
----------------------------
upload.aspx.vb的內容:
Imports System Imports System.Data Imports System.Configuration Imports System.Collections Imports System.Web Imports System.Web.Security Imports System.Web.UI Imports System.Web.UI.WebControls Imports System.Web.UI.WebControls.WebParts Imports System.Web.UI.HtmlControls Imports System.Collections.Generic Imports System.IO Partial Class upload Inherits System.Web.UI.Page Public Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs) Dim thumbnail_image As System.Drawing.Image = Nothing Dim original_image As System.Drawing.Image = Nothing Dim final_image As System.Drawing.Bitmap = Nothing Dim graphic As System.Drawing.Graphics = Nothing Dim ms As System.IO.MemoryStream = Nothing Try Dim jpeg_image_upload As HttpPostedFile = Request.Files("Filedata") Dim savePath As String = "C:\upload\" '這裡請設定要存檔的路徑 Dim fileName As String = jpeg_image_upload.FileName savePath &= fileName jpeg_image_upload.SaveAs(savePath) original_image = System.Drawing.Image.FromStream(jpeg_image_upload.InputStream) Dim width As Integer = original_image.Width Dim height As Integer = original_image.Height Dim target_width As Integer = 100 Dim target_height As Integer = 100 Dim new_width, new_height As Integer Dim target_ratio As Double = target_width / target_height Dim image_ratio As Double = width / height If target_ratio > image_ratio Then new_height = target_height new_width = Math.Floor(image_ratio * target_height) Else new_height = Math.Floor(target_width / image_ratio) new_width = target_width End If final_image = New System.Drawing.Bitmap(target_width, target_height) graphic = System.Drawing.Graphics.FromImage(final_image) graphic.FillRectangle(New System.Drawing.SolidBrush(System.Drawing.Color.Black), New System.Drawing.Rectangle(0, 0, target_width, target_height)) Dim paste_x As Integer = (target_width - new_width) / 2 Dim paste_y As Integer = (target_height - new_height) / 2 graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic '/* new way */ '//graphic.DrawImage(thumbnail_image, paste_x, paste_y, new_width, new_height) graphic.DrawImage(original_image, paste_x, paste_y, new_width, new_height) ' // Store the thumbnail in the session (Note: this is bad, it will take a lot of memory, but this is just a demo) 'final_image.Save("C:\upload\") MS = New System.IO.MemoryStream() final_image.Save(MS, System.Drawing.Imaging.ImageFormat.Jpeg) ' // Store the data in my custom Thumbnail object Dim thumbnail_id As String = DateTime.Now.ToString("yyyyMMddHHmmssfff") Dim thumb As Thumbnail = New Thumbnail(thumbnail_id, ms.GetBuffer()) ' // Put it all in the Session (initialize the session if necessary) Dim thumbnails As System.Collections.Generic.List(Of Thumbnail) = Session("file_info") If thumbnails Is Nothing Then thumbnails = New System.Collections.Generic.List(Of Thumbnail)() Session("file_info") = thumbnails End If thumbnails.Add(thumb) Response.StatusCode = 200 Response.Write(thumbnail_id) Catch ex As Exception ' // If any kind of error occurs return a 500 Internal Server error Response.Write(ex.Message) 'Response.StatusCode = 500 'Response.Write("An error occured") 'Response.End() Finally ' // Clean up If Not final_image Is Nothing Then final_image.Dispose() End If If Not graphic Is Nothing Then graphic.Dispose() End If If Not original_image Is Nothing Then original_image.Dispose() End If If Not thumbnail_image Is Nothing Then thumbnail_image.Dispose() End If If Not ms Is Nothing Then ms.Close() End If Response.End() End Try End Sub End Class
------------------------------------------------------------------------------------
大致上只需要改這三個檔案,就可以看到上傳的效果了
但請注意,檔案的權限要改成能上傳的權限
另外這個還沒辦法另存縮圖,如果小弟研究好了,會一起放上來分享
全站熱搜
留言列表