摘要:電子商務(wù)網(wǎng)站與普通網(wǎng)站相比有很多特點(diǎn),在安全性能、負(fù)載性能、商品圖片遠(yuǎn)程提交、網(wǎng)站頁面國際化等方面都有特殊要求。而該文主要對商務(wù)網(wǎng)站后兩個方面的應(yīng)用設(shè)計(jì)進(jìn)行了簡單分析,設(shè)計(jì)平臺為VS.NET+SQL Server。實(shí)際上,該文介紹的方法對其他有圖形圖像遠(yuǎn)程管理和頁面需要國際化處理的網(wǎng)站普遍適用。
關(guān)鍵詞:電子商務(wù)網(wǎng)站;實(shí)用技術(shù);圖片上傳;網(wǎng)頁國際化
中圖分類號:TP393文獻(xiàn)標(biāo)識碼:A 文章編號:1009-3044(2010)12-2826-03
Two Practical Skills of Ecommerce Website
ZHOU Su-xia, WEI Wen-shen
(Three Gorges Vocational and Technical College, Yichang 443000, China)
Abstract: E-commerce site has many characteristics compared with ordinary web site in safety performance,load performance,product images remote submission,internationalization of web site page and other aspects of special requirements. The paper mainly makes a simple application of the analysis on two aspects of post-Commerce website design,design platform being VS.NET + SQL Server.In fact,the article describes the method for remote management of other graphic images and page processing site in need of international universal application.
Key words: ecommerce website; practical skill; images upload; homepage internationalization
1 圖片遠(yuǎn)程提交
這里介紹的方法是使用服務(wù)器上SQL Server數(shù)據(jù)庫的Image型字段保存來自客戶端提交的圖片信息,該方法具有讀寫速度快、安全性好、通用性強(qiáng)等優(yōu)點(diǎn),在電子商務(wù)或其它有圖片上傳需求的應(yīng)用中應(yīng)用非常廣泛。
1.1 圖片提交代碼
'下面fs為文件域類型的HTML控件的id,接收圖片文件名的輸入或?yàn)g覽選擇,本代碼只接收30KB以內(nèi)的文件,該值可由用戶隨意調(diào)整
If fs.Value <> \"\" Then
intImageSize = fs.PostedFile.ContentLength '獲得圖像文件大小(字節(jié)數(shù))
strImageType = Right(fs.PostedFile.ContentType, 3) ' 獲得圖像文件類型
If strImageType <> \"gif\" And strImageType <> \"peg\" Then
Response.Write(\"\")
Else
If strImageType = \"gif\" Then
strImageType = \".gif\"
Else
strImageType = \".jpg\"
End If
If intImageSize > 30 * 1024 Then
Response.Write(\"\")
Else
Dim fs1 As FileStream
Dim gcname As String
Dim gcname2 As String
ImageStream = fs.PostedFile.InputStream '獲得文件流式句柄
'分配文件大小的緩存(字節(jié)數(shù))
Dim ImageContent(intImageSize) As Byte
Dim intStatus As Integer
'讀取圖片數(shù)據(jù)到緩存ImageContent中
intStatus = ImageStream.Read(ImageContent, 0, intImageSize)
' 調(diào)用存儲過程UP_WY_ADD1插入新記錄并保存圖片數(shù)據(jù)到服務(wù)器數(shù)據(jù)庫中
Dim myCommand As New SqlCommand(\"UP_WY_ADD1\", connection)
myCommand.CommandType = CommandType.StoredProcedure ' 建立存儲過程
' 為存儲過程傳遞新記錄字段參數(shù),設(shè)記錄有文件名、圖片、大小三個字段
Dim prmPersonImageType As New SqlParameter(\"@文件名\", SqlDbType.Char, 22)
prmPersonImageType.Value = strImageType
myCommand.Parameters.Add(prmPersonImageType)
'將緩存ImageContent中的圖片數(shù)據(jù)寫到“圖片”字段(Image類型)中
Dim prmPersonImage As New SqlParameter(\"@圖片\", SqlDbType.Image)
prmPersonImage.Value = ImageContent
myCommand.Parameters.Add(prmPersonImage)
Dim prmPersonImage1 As New SqlParameter(\"@大小\", SqlDbType.Int)
prmPersonImage1.Value = intImageSize
myCommand.Parameters.Add(prmPersonImage1)
connection.Open()
myCommand.ExecuteNonQuery()'運(yùn)行存儲過程,添加記錄,保存圖片數(shù)據(jù)
Response.Write(\"\")
End If
End If
End If
1.2 存儲過程代碼
CREATEPROCEDUREUP_WY_ADD1
@文件名 char(22),
@圖片 image,
@大小 int,
AS
INSERT INTO tk(文件名,圖片,大小) VALUES(@文件名,@圖片,@大小)
update tk set 文件名= str(@@identity,4)+文件名 where id=@@identity
GO
2 網(wǎng)站頁面國際化
電子商務(wù)不受時空限制,要讓全球任何國家和地區(qū)的人們能閱讀你的網(wǎng)站內(nèi)容,頁面國際化
就是必須的,這里主要介紹VS.NET頁面的國際化,其他頁面國際化方法類似。
國際化.NET頁面時,Visual Studio 系統(tǒng)不會自動產(chǎn)生資源文件,我們必須手動建立和編輯 XML(resx) 資源文件。
為使問題簡化,下面只介紹如何建立中英雙語頁面,同時討論如何編寫存取這些資源的程序代碼。我們可以使用XML建立并編輯英文、中文資源文件,當(dāng)然也能將文本內(nèi)容轉(zhuǎn)換成相應(yīng)的資源文件。接下來為圖1所示的界面建立英文界面,操作步驟如下:
第1步啟動VS.NET,打開Web應(yīng)用項(xiàng)目;
第2步右擊項(xiàng)目名,選擇“添加”/“添加新項(xiàng)...”命令菜單,在模板中選擇“程序集資源文件”,文件名設(shè)為strings.resx。資源文件strings.resx包含英文的原有資源,每當(dāng)應(yīng)用程序找不到更適合的界面顯示資源時,就會存取這些資源。此時strings.resx文件被加入到項(xiàng)目中,雙擊打開它,如圖2所示。
第3步在“數(shù)據(jù)表”列表中,選擇“data”,在“數(shù)據(jù)”(data的數(shù)據(jù))窗口中增加2行數(shù)據(jù)(其余數(shù)據(jù)添加方法相同),如下所示:
name value
--------------
user username
pass psaaword
第4步重復(fù)第2和第3步,建立另外一個名為strings.zh-CHS.resx的資料文件,數(shù)據(jù)如下:
name value
--------------
user 用戶
pass 密碼
第5步在頁面代碼開始處(主類前面)導(dǎo)入System.Resources、System.Globalization和System.Threading類名空間。代碼如下:
VB中:
Imports System.Resources
Imports System.Globalization
Imports System.Threading
C#中:
using System.Resources;
using System.Globalization;
using System.Threading;
第6步在主類中所有過程之前聲明ResourceManager類的變量LocRM,代碼如下:
VB中:
protected LocRM As ResourceManager
C#中:
protected ResourceManager LocRM;
第7步將以下代碼加入到頁面的Page_Init(頁面初始化)過程中,指定網(wǎng)頁按照瀏覽器的語言特性(首選項(xiàng))來顯示資源和格式。如果省略此代碼,Web 應(yīng)用程序就會按服務(wù)器的當(dāng)前語言設(shè)置,這可能對客戶端使用者并不適用。
VB中:
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture (Request.
UserLanguages(0))
Thread.CurrentThread.CurrentUICulture = New CultureInfo(Request.UserLanguages(0))
C#中:
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Request.
UserLanguages[0]);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(Request.UserLanguages[0]);
第8步假設(shè)頁面上有Label1和Label2兩個控件,我們將下列代碼加入 Page_Load 方法的起始處。
VB中:
LocRM = New ResourceManager(\"WebApplication1.strings\", GetType(WebForm1).Assembly)
Label1.Text=LocRM.GetString(\"user\")
Label2.Text=LocRM.GetString(\"pass\")
C#中:
LocRM= new ResourceManager(\"WebApplication1.strings\", typeof(WebForm1).Assembly);
Label1.Text=LocRM.GetString(\"user\");
Label2.Text=LocRM.GetString(\"pass\");
操作完成,編譯、執(zhí)行就可以看到結(jié)果。IE語言屬性中,如果選擇中文(中國),就會顯示中文網(wǎng)頁;如果選擇英語-美國(en-us)等其它語種則會顯示英文網(wǎng)頁(如圖3所示),這比做中英兩套網(wǎng)頁要大大節(jié)省空間并利于維護(hù)。
3 結(jié)束語
本文通過對電子商務(wù)網(wǎng)站設(shè)計(jì)中有關(guān)商品圖片的遠(yuǎn)程提交及VS.NET頁面國際化的方法作了基本的介紹,讀者可以舉一反三加以應(yīng)用,同時本文通過拋磚引玉,希望能與各位讀者探討相關(guān)方法與技巧。
參考文獻(xiàn):
[1] 周蘇峽,李建利.Web數(shù)據(jù)庫應(yīng)用實(shí)例教程[M].北京:北京交通大學(xué)出版社,2008:197.
[2] 周蘇峽,陳文明.網(wǎng)頁設(shè)計(jì)實(shí)用教程[M].2版.北京:北京交通大學(xué)出版社,2010:89-96.
[3] 郝思嘉.ASP.NET課程設(shè)計(jì)案例精編[M].北京:中國水利水電出版社,2006:195-196.