• 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>

  • 相关阅读:
    L2-011 玩转二叉树 二叉树
    L2-010 排座位 并查集
    L2-009 抢红包
    VS 编译报错:意外的字符
    关于js的类型转换
    github相关操作总结
    关于时间的相关处理
    uniapp实现简单的动画效果(不使用dom操作)
    uniapp选择日期
    vue使用音频组件
  • 原文地址:https://www.cnblogs.com/cf924823/p/5081497.html
Copyright © 2020-2023  润新知