zoukankan      html  css  js  c++  java
  • 11.27 上传下载 图片预览

    代码页面

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public partial class Default2 : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {

    //1.找到要上传的文件名
    string name = FileUpload1.FileName.ToString();

    //2.处理文件名 ,防止上传的文件名被覆盖,
    string clname = DateTime.Now.ToString("yyyyMMddhhmmssms")+name;


    //3.造一个要保存的路径
    string path = @"Files" + clname;

    //处理相对路径为绝对路径。是一个映射
    string jdpath = Server.MapPath(path);

    //5,保存
    FileUpload1.SaveAs(jdpath);

    }
    protected void Button2_Click(object sender, EventArgs e)
    {

    //1.找到要上传的文件名
    string name = PicLoad.FileName.ToString();

    //2.处理文件名 ,防止上传的文件名被覆盖,
    string clname = DateTime.Now.ToString("yyyyMMddhhmmssms") + name;


    //3.造一个要保存的路径
    string path = @"image" + clname;

    //处理相对路径为绝对路径。是一个映射
    string jdpath = Server.MapPath(path);

    //5,保存
    PicLoad.SaveAs(jdpath);
    }
    }

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>

    <br />
    <asp:FileUpload ID="FileUpload1" runat="server" />
    &nbsp;&nbsp;
    <asp:Button ID="Button1" runat="server" Text="上传" OnClick="Button1_Click" style="height: 21px" />
    <br />
    <br />

    <asp:FileUpload ID="FileUpload2" runat="server" />
    &nbsp;&nbsp;
    <asp:Button ID="Button2" runat="server" OnClick="Button1_Click" Text="上传" />
    &nbsp;
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <br />
    <br />
    <br />

    <div id="localImag" style=" 254px; height: 200px">
    <img id="preview" alt="点击上传预览图片" src="images/qiye.png" width="200" height="200" />
    <asp:FileUpload ID="PicLoad" Height="200px" style="position:relative; top:-200px; opacity:0; left: 0px; 249px;" runat="server" onchange="javascript:setImagePreview(this,localImag,preview);"></asp:FileUpload>
    </div>
    <asp:Button ID="Button3" runat="server" OnClick="Button2_Click" Text="上传" />

    <script type="text/javascript">
    function setImagePreview(docObj, localImagId, imgObjPreview) {
    if (docObj.files && docObj.files[0]) {
    //火狐下,直接设img属性
    imgObjPreview.style.display = 'block';
    imgObjPreview.style.width = '200px';
    imgObjPreview.style.height = '200px';


    //火狐7以上版本不能用上面的getAsDataURL()方式获取,需要一下方式
    imgObjPreview.src = window.URL.createObjectURL(docObj.files[0]);
    }
    else {
    //IE下,使用滤镜
    docObj.select();
    var imgSrc = document.selection.createRange().text;

    //必须设置初始大小
    localImagId.style.width = "200px";
    localImagId.style.height = "200px";

    //图片异常的捕捉,防止用户修改后缀来伪造图片
    try {
    localImagId.style.filter =
    "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
    localImagId.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src
    = imgSrc;
    }
    catch (e) {
    alert("您上传的图片格式不正确,请重新选择!");
    return false;
    }
    imgObjPreview.style.display = 'none';
    document.selection.empty();
    }
    return true;
    }
    </script>

    <br />
    <br />
    <br />
    <a href="Files/201512270925202520数据库1.xlsx">下载</a>
    </div>
    </form>
    </body>
    </html>

  • 相关阅读:
    linux清理内存
    华为代码注释标准
    Spring缓存机制的理解
    jQuery实现动态分割div—通过拖动分隔栏实现上下、左右动态改变左右、上下两个相邻div的大小
    模仿iframe框架,由分隔栏动态改变左右两侧div大小———基于jQuery
    js实现由分隔栏决定两侧div的大小—js动态分割div
    java基于socket的简单聊天系统
    中国移动归属地区号大全
    将本地光盘做成yum源
    windows下设置计划任务自动执行PHP脚本
  • 原文地址:https://www.cnblogs.com/cf924823/p/5081497.html
Copyright © 2011-2022 走看看