【IT168 技术文档】
以前,通过ASP上传图象(图象的大小、类型都受到限制)一般都是要借助外部组件来完成,.NET的出现,使这一工作变得非常容易并且可以随便的使用Bitmap和Image类型。
在这个指导思想下,我将按照以下步骤(在你要上传图象文件上)创建一个简单的WEB窗体,该窗体将判断上传的文件是否是JPEG文件、判断该文件是否存在(必要时你可以重命名)。
1、 创建一个新Web 应用程序项目;
2、 打开Web 窗体;
3、 在窗体上面添加一个HTML表单,并把它转换成服务器控件。在这个例子里,该文件将命名为filUpload;(把HTML转换成服务器控件的方法是,在它的上面右击鼠标然后选择Run As Server Control)
4、 切换到HTML view并添加/更改FORM标签的enctype属性为multipart/form-data。如:enctype="multipart/form-data"。
5、 在Web窗体上添加一个BUTTON并命名为btnUpload。
6、 向Web应用程序添加一个folder called /images。
7、 在窗体上添加一个Web Form Image并命名为imgPicture,设置宽度和高度分别为160和120。
8、 添加一个Label控件并命名为lblOutput。显示当在上传的过程中发生的任何错误。
9、 给按钮btnUpload的单击事件添加如下代码:(如果你想分析以下代码的细节,你可以把下面的代码复制粘贴到VS.NET IDE集成开发环境。)
private void btnUpload_Click(object sender, System.EventArgs e) { // Initialize variables string sSavePath; string sThumbExtension; int intThumbWidth; int intThumbHeight; // Set constant values sSavePath = "images/"; sThumbExtension = "_thumb"; intThumbWidth = 160; intThumbHeight = 120; // If file field isn’t empty if (filUpload.PostedFile != null) { // Check file size (mustn’t be 0) HttpPostedFile myFile = filUpload.PostedFile; int nFileLen = myFile.ContentLength; if (nFileLen == 0) { lblOutput.Text = "No file was uploaded."; return; } // Check file extension (must be JPG) if (System.IO.Path.GetExtension(myFile.FileName).ToLower() != ".jpg") { lblOutput.Text = "The file must have an extension of JPG"; return; } // Read file into a data stream byte[] myData = new Byte[nFileLen]; myFile.InputStream.Read(myData,0,nFileLen); // Make sure a duplicate file doesn’t exist. If it does, keep on appending an // incremental numeric until it is unique string sFilename = System.IO.Path.GetFileName(myFile.FileName); int file_append = 0; while (System.IO.File.Exists(Server.MapPath(sSavePath + sFilename))) { file_append++; sFilename = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) + file_append.ToString() + ".jpg"; } // Save the stream to disk System.IO.FileStream newFile = new System.IO.FileStream(Server.MapPath(sSavePath + sFilename), System.IO.FileMode.Create); newFile.Write(myData,0, myData.Length); newFile.Close(); // Check whether the file is really a JPEG by opening it System.Drawing.Image.GetThumbnailImageAbort myCallBack = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback); Bitmap myBitmap; try { myBitmap = new Bitmap(Server.MapPath(sSavePath + sFilename)); // If jpg file is a jpeg, create a thumbnail filename that is unique. file_append = 0; string sThumbFile = System.IO.Path.GetFileNameWithoutExtension,myFile.FileName) + sThumbExtension + ".jpg"; while (System.IO.File.Exists(Server.MapPath(sSavePath + sThumbFile))) { file_append++; sThumbFile = System.IO.Path.GetFileNameWithoutExtension(myFile.FileName) + file_append.ToString() + sThumbExtension + ".jpg"; } // Save thumbnail and output it onto the webpage System.Drawing.Image myThumbnail = myBitmap.GetThumbnailImage(intThumbWidth, intThumbHeight, myCallBack, IntPtr.Zero); myThumbnail.Save (Server.MapPath(sSavePath + sThumbFile)); imgPicture.ImageUrl = sSavePath + sThumbFile; // Displaying success information lblOutput.Text = "File uploaded successfully!"; // Destroy objects myThumbnail.Dispose(); myBitmap.Dispose(); } catch (ArgumentException errArgument) { // The file wasn't a valid jpg file lblOutput.Text = "The file wasn't a valid jpg file."; System.IO.File.Delete(Server.MapPath(sSavePath + sFilename)); } } } public bool ThumbnailCallback() { return false; }
10.运行以上创建的 Web页(webpage),并分别使用JPG文件和其他类型的文件来测试错误检查(error-checking)机制。