FileLoad附件上传本地

版权声明:Till good is better, but better best !My trying hard will go on!Please wait and see! https://blog.csdn.net/u012761373/article/details/41895015

1.简单的UpFileLoad上传(本地)

Page:

<pre class="html" name="code"><div>
        <table>
            <tr>
                <th>附件:</th>
                <td><asp:FileUpload ID="ImgFileUpload1" runat="server" /></td>
            </tr>
            <tr>
                <td><asp:Button ID="btnSaveImmg" runat="server" Text="保存" OnClick="btnSaveImmg_Click" /></td>
            </tr>
        </table>
    </div>

 
 

Code:

	//获取全路径 
            string fullFileName = ImgFileUpload1.PostedFile.FileName.ToString();
            //获取文件名
            string fileName = ImgFileUpload1.FileName.ToString();
            //获取文件类型 string typeImg = System.IO.Path.GetExtension(fullFileName);
            string type = fileName.Substring(fileName.LastIndexOf(".") + 1);
            //获取上传路径
            string path = Server.MapPath("image\\") + fileName;
            Response.Write(path);
            //文件大小
            FileInfo file = new FileInfo(fullFileName);
            float size = (file.Length / 1024) / 1204;
            if (File.Exists(path))
            {
                Response.Write("<script language='javascript'>alert('图片存在')</script>");
                return;
            }
            else
            {
                if (type == "jpg" || type == "mp3")
                {
                    if (size > 4)
                    {
                        Response.Write("<script language='javascript'>alert('图片不允许大于4MB')</script>");
                        return;
                    }
                    else
                    {
                        ImgFileUpload1.PostedFile.SaveAs(path);
                        Response.Write("<script language='javascript'>alert('上传成功')</script>");
                    }
                }
                else
                {
                    Response.Write("<script language='javascript'>alert('图片格式错误')</script>");
                }
            }
        }

2.网络上的图片下载下了到本地相对路径

Page:

<body>
    <form id="form1" runat="server">
    <div>
        <table>
            <tr>
                <td><asp:TextBox ID="txtGetDirectory" runat="server" Text="输入获取文件路径"></asp:TextBox></td>
            </tr>
            <tr>
                <td><asp:TextBox ID="txtSaveDirectory" runat="server" Text="输入保存文件路径"></asp:TextBox></td>
            </tr>
            <tr>
                <td><asp:Button ID="btnGetRequest" runat="server" Text="保存网络上的图片到本地" OnClick="btnGetRequest_Click" /></td>
            </tr>
        </table>
    </div>
    <div>
        执行状态 :
        <asp:Label ID="lblInfo" runat="server" Text=""></asp:Label>
        执行时间 :
        <asp:Label ID="lblDate" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>

Code:

System.Diagnostics.Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            HttpWebRequestDataInit();
            stopwatch.Stop();
            TimeSpan ts = stopwatch.Elapsed;
            //时:分:秒:毫秒
            // Format and display the TimeSpan value.
            string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10);
            lblDate.Text = elapsedTime;

        }

        protected void HttpWebRequestDataInit()
        {
            string uploadUrl = txtGetDirectory.Text;
            string downloadUrl = txtSaveDirectory.Text;
            if (uploadUrl.Length < 1)
            {
                //输入获取文件路径
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script languege='javascript'>alert('输入获取文件路径');</script>");
                return;
                
            }
            if (downloadUrl.Length < 1)
            {
                //输入保存文件路径
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script languege='javascript'>alert('输入保存文件路径');</script>");
                return;

            }
            if (!System.IO.Directory.Exists(downloadUrl))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script languege='javascript'>alert('输入保存文件路径不存在');</script>");
                return;
            }
            DateTime date = DateTime.Now;
            //创建一个Web请求,返回HttpWebRequest对象
            HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create(uploadUrl);
            //设置接收对象的范围为0-10000000字节。
            hwr.AddRange(0, 10000000);
            using (Stream inStream = hwr.GetResponse().GetResponseStream())
            {
                if (System.IO.Directory.Exists(downloadUrl))
                {
                    string urlImageType = uploadUrl.Substring(uploadUrl.LastIndexOf("."), uploadUrl.Length - uploadUrl.LastIndexOf("."));
                    //这里作出判断 避免网络上没有显示后缀的图片
                    if (urlImageType.Length < 1 || urlImageType.Length > 4)
                    {
                        urlImageType = ".jpg";
                    }
                    string urlImageName = uploadUrl.Substring(uploadUrl.LastIndexOf("/"), uploadUrl.Length - uploadUrl.LastIndexOf("/")).Replace("/", "");
                    string TempdownloadUrl = downloadUrl + date.ToString().Replace("/", "").Replace(":", "").Replace(" ","") + urlImageType;
                    if (Topevery_DUM_Bloss.UtilityHelper.GetAttachType(urlImageType)==0)
                    {
                        using (FileStream fs = File.Create(TempdownloadUrl))
                        {
                            try
                            {
                                //建立字节组,并设置它的大小是多少字节
                                byte[] bytes = new byte[102400];
                                int n = 1;
                                while (n > 0)
                                {
                                    //一次从流中读多少字节,并把值赋给N,当读完后,N为0,并退出循环
                                    n = inStream.Read(bytes, 0, 10240);
                                    fs.Write(bytes, 0, n); //将指定字节的流信息写入文件流中
                                }
                            }
                            catch (Exception ex)
                            {
                                //LogHelper.Error("FMP错误", ex);
                                throw ex;
                            }
                            finally
                            {
                                if (inStream != null)
                                {
                                    inStream.Close();
                                    inStream.Dispose();
                                }
                            }
                            lblInfo.Text = "执行成功";
                        }
                    }

                }
                else
                {
                    lblInfo.Text = "找不到文件路径,未执行成功";
                }
                
            }
             
        }

Topevery_DUM_Bloss.UtilityHelper.GetAttachType(urlImageType)
public static Int32 GetAttachType(string type)
        {
            Int32 attachType = 3;
            switch (type.ToLower())
            {
                case ".jpg":
                case ".gif":
                case ".png":
                case ".bmp":
                case ".jpeg":
                    attachType = 0;
                    break;
                case ".wav":
                case ".mp3":
                case ".wma":
                case ".ram":
                case "amr":
                    attachType = 1;
                    break;
                case ".mpeg":
                case ".rm":
                case ".wmv":
                case ".avi":
                case ".mp4":
                case ".mts":
                    attachType = 2;
                    break;
            }
            return attachType;
        }






猜你喜欢

转载自blog.csdn.net/u012761373/article/details/41895015