3.1 基于Web方式的图片上传Web-based File Uploading
在很多实时应用程序中,例如在线相册,在图片被重设大小之前,它已经被用户上传到Web服务器上。为了得到上传的图片,必须用到服务器端上传组件例如Persites软件出品的AspUpload ®。当AspJpeg与任意上传组件合用时(并不一定要求是AspUpload),不过我们的讨论和样例代码基于AspUpload。如果想得到AspUpload的更多信息,或者想下载一个AspUpload的30天免费体验版,请访问www.aspupload.comIn many real-life applications such as online photo albums, before an image is resized it has to be uploaded to the Web server by the user. To capture uploaded images, a server-side upload component such as Persits Software's AspUpload ® has to be used. While AspJpeg can be used in tandem with any upload component, not just AspUpload, our discussion and code samples will be based on AspUpload. For more information about AspUpload, and to download your free 30-day evaluation copy, visit www.aspupload.com.
通过上传方式把一个或者多个文件从客户机上传到服务器,需要用一个<Form>,设置属性Enctype="multipart/form-data",并用一个或者多个FROM子项<input type="file">为了得到上传的文件并用AspUpload把它们保存到服务器里,下面的一些代码可能是有用的:One or more files can be uploaded from the user's machine to the server using a <FORM> with the attribute ENCTYPE="multipart/form-data" and one or more <INPUT TYPE="FILE"> form items. To capture the uploaded files and save them on the server using AspUpload, one may use the following code:
<FORM ENCTYPE="multipart/form-data" ACTION="upload.asp">
<INPUT TYPE="FILE" NAME="FILE1">
<INPUT TYPE="FILE" NAME="FILE2">
<INPUT TYPE="FILE" NAME="FILE3">
<INPUT TYPE="SUBMIT" VALUE="Upload!">
</FORM>
upload.asp:
<%
Set Upload = Server.CreateObject("Persits.Upload")
' Save uploaded files
Upload.Open "c:\upload"
' Display paths of uploaded files
For Each File in Upload.Files
Response.Write File.Path & "<BR>"
Next
%>
除了HTML的Form之外,文件还能够通过使用第三方ActiveX客户端控件以及能够提供html Form所不具备的功能的Java小程序上传到服务器,这些功能包括上传一整个目录,或者用拖放上传。这些产品是我们研究讨论范围之外的。因此我们只关注服务器端脚本,如果想得到更多的关于客户端上传控件XUpload和JUPload,请浏览AspUpload.com的网站。In addition to HTML forms, files can also be uploaded to the server using various 3rd-party client-side ActiveX controls and Java applets which provide additional features not found in the forms, such as the ability to upload an entire directory, or dragging-and-dropping. These products are outside the scope of this discussion, as we focus on the server-side scripting only. For more information about the client-side upload agents XUpload and JUpload, see the AspUpload.com web site.
3.2 转换上传的图片Resizing Uploaded Images
以下样例代码使你能够上传一个图片,重设它的大小,保存原图和缩图片到Microsoft Access数据库文件aspjpeg.mdb中,注意这些代码实例先保存缩略图到磁盘中,然后再重新打开它保存到数据库里。直接的内存到数据库保存过程在3.3节中展开讨论。The following code sample enables you to upload an image, resize it, and then save the original and its thumbnail in the MS Access database file aspjpeg.mdb. Notice that this code sample saves the thumbnail to disk and then re-opens it for a database save. A direct memory-to-database procedure is covered in Section 3.3.
在运行这个代码实例之前,确保AspUpload被安装在你的服务器里,并用Windows资源管理器在数据库文件aspjpeg.mdb所在的子目录\Samples\Ds里给予“Everyone”用户组完全控制的权限,否则上传过程中可能会发生一个运行期错误。Before running this code sample, make sure AspUpload is installed on your server, and use Windows Explorer to give the "Everyone" group full control over the database file aspjpeg.mdb located in the subdirectory \Samples\Db, or a run-time error may occur during uploading.
VB Script
<%
' Create an instance of AspUpload object
Set Upload = Server.CreateObject("Persits.Upload")
' Compute path to save uploaded files to
Path = Server.MapPath(".")
' Capture uploaded file. Return the number of files uploaded
Count = Upload.Save(Path)
If Count = 0 Then
Response.Write "No images selected."
Response.End
Else
' Obtain File object representing uploaded file
Set File = Upload.Files(1)
' Is this a valid image file?
If File.ImageType <> "UNKNOWN" Then
' Create instance of AspJpeg object
Set jpeg = Server.CreateObject("Persits.Jpeg")
' Open uploaded file
jpeg.Open( File.Path )
' Resize image according to "scale" option.
' We cannot use Request.Form, so we use Upload.Form instead.
jpeg.Width = jpeg.OriginalWidth * Upload.Form("scale") / 100
jpeg.Height = jpeg.OriginalHeight * Upload.Form("scale") / 100
SavePath = Path & "\small_" & File.ExtractFileName
' AspJpeg always generates thumbnails in JPEG format.
' If the original file was not a JPEG, append .JPG ext.
If UCase(Right(SavePath, 3)) <> "JPG" Then
SavePath = SavePath & ".jpg"
End If
jpeg.Save SavePath
' Save both images in the database along with description.
strConnect = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath("../db/aspjpeg.mdb")
Set rs = Server.CreateObject("adodb.recordset")
rs.Open "images", strConnect, 1, 3
rs.AddNew
' Use File.Binary to access binary data of uploaded file.
rs("original_image").Value = File.Binary
Set ThumbFile = Upload.OpenFile(SavePath)
rs("thumbnail").Value = ThumbFile.Binary
rs("description") = Upload.Form("Description")
rs.Update
rs.Close
Set rs = Nothing
Response.Write "Success!"
Else
Response.Write "This is not a valid image."
Response.End
End If
End If
%>
C#
<script runat="server" LANGUAGE="C#">
void Page_Load(Object Source, EventArgs E)
{
// Create an instance of AspUpload object
IUploadManager objUpload;
objUpload = new UploadManager();
String strPath = Server.MapPath(".");
// Save returns the number of uploaded files
int nCount = objUpload.Save(strPath, Missing.Value, Missing.Value);
if( nCount == 0 )
{
txtMsg.InnerHtml = "No images selected.";
return;
}
// Obtain File object representing uploaded file
IUploadedFile objFile;
objFile = objUpload.Files.Item(1);
// Is this a valid image file?
if( objFile.ImageType != "UNKNOWN" )
{
// Create instance of AspJpeg object
IASPJpeg objJpeg;
objJpeg = new ASPJpeg();
// Open uploaded file
objJpeg.Open( objFile.Path );
// Resize image according to "scale" option.
// We cannot use Request.Form, so we use Upload.Form instead.
int nScale = int.Parse(objUpload.Form.Item("scale").Value);
objJpeg.Width = objJpeg.OriginalWidth * nScale / 100;
objJpeg.Height = objJpeg.OriginalHeight * nScale / 100;
String strSavePath = strPath + "\\small_" + objFile.ExtractFileName();
// AspJpeg always generates thumbnails in JPEG format.
// If the original file was not a JPEG, append .JPG extension.
if( strSavePath.ToUpper().Substring( strSavePath.Length - 3) != "JPG" )
{
strSavePath += ".jpg";
}
objJpeg.Save( strSavePath );
// Save both images in the database along with description.
String strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("../db/aspjpeg.mdb");
OleDbConnection myConnection = new OleDbConnection(strConn);
myConnection.Open();
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter ("select * from images", myConnection);
DataSet myDataSet = new DataSet();
myDataAdapter.Fill( myDataSet, "images" );
DataTable tblImages = myDataSet.Tables["images"];
DataRow rowImage;
// Add a new row
rowImage = tblImages.NewRow();
tblImages.Rows.Add( rowImage );
rowImage.BeginEdit();
// Save original image and thumbnail in the database table.
rowImage["original_image"] = objFile.Binary;
IUploadedFile objThumb = objUpload.OpenFile( strSavePath );
rowImage["thumbnail"] = objThumb.Binary;
rowImage["description"] = objUpload.Form.Item("description").Value;
rowImage.EndEdit();
// Without this line, Update will fail.
OleDbCommandBuilder myCB = new OleDbCommandBuilder(myDataAdapter);
myDataAdapter.Update( myDataSet, "images" );
myConnection.Close();
txtMsg.InnerHtml = "Success!";
}
else
{
txtMsg.InnerHtml = "This is not a valid image.";
}
}
</script>
点击下例链接来运行这些样例代码:Click the links below to run this code sample:
http://localhost/aspjpeg/manual_03/03_form.asp
http://localhost/aspjpeg/manual_03/03_form.aspx




上一篇
下一篇


文章来自:
Tags: 





