3.3 上传到内存里Memory Uploads
AspUpload也能够把上传上去的图片保存到内存中而不保存到磁盘上,又正好AspJpeg能够把图片从内存中打开,并保存生成的缩略图到内存中,所以这整个“上传->重设大小->保存到数据库”过程能够在不在服务器硬盘里生成临时文件的情况下进行,它能够提高性能,节省磁盘空间,改善安全性。AspUpload is capable of saving uploaded files to memory as opposed to disk. Since AspJpeg can open images from memory, and also save the resultant thumbnails to memory, the entire "upload -> resize -> save in the database" process can be performed without creating temporary files on the server's hard drive which enhances performance, conserves disk space and improves security.
以下代码实例类似于上一个,但是这次我们是用AspUpload的内存上传功能(这种保存方法也被称为无路径算法),一张上传的图片通过Jpeg.OpenBinary方法打开,(File.binary),并且生成的缩略图直接通过rs("image").Value=Jpeg.Binary方法保存到记录中:The following code sample is similar to the previous one, except that we use the memory upload feature of AspUpload (the Save method is called without a Path argument), an uploaded image is opened via Jpeg.OpenBinary( File.Binary ), and the resultant thumbnail is saved directly to the recordset via rs("image").Value = Jpeg.Binary:
VB Script:
<%
...
Count = Upload.Save
' Open uploaded file from memory
jpeg.OpenBinary( File.Binary )
' For now, Jpeg.Binary contains the original image
rs("original_image").Value = Jpeg.Binary
...
jpeg.Width = jpeg.OriginalWidth * Upload.Form("scale") / 100
jpeg.Height = jpeg.OriginalHeight * Upload.Form("scale") / 100
' Now Jpeg contains a resized version of the original file.
rs("thumbnail").Value = Jpeg.Binary
...
%>
C#:
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Reflection" %>
<%@ Import Namespace="ASPJPEGLib" %>
<%@ Page aspCompat="True" %>
<script runat="server" LANGUAGE="C#">
void Page_Load(Object Source, EventArgs E)
{
...
// Save to memory, return the number of uploaded files
int nCount = objUpload.Save(Missing.Value, Missing.Value, Missing.Value);
...
// objJpeg contains original image
rowImage["original_image"] = objJpeg.Binary;
// 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;
// Now objJpeg contains resized version of original image
rowImage["thumbnail"] = objJpeg.Binary;
...
}
</script>
点击下例链接来运行这些样例代码:Click the links below to run this code sample:
http://localhost/aspjpeg/manual_03/03_form2mem.asp
http://localhost/aspjpeg/manual_03/03_form2mem.aspx




上一篇
下一篇


文章来自:
Tags: 





