Showing posts with label upload. Show all posts
Showing posts with label upload. Show all posts

8/29/11

Upload and Show PDF in Website

Just follow these steps.

1) Create a Class Library project. I create a Visual C# Class Library project using Visual Studio and select C# as language and give it a name. I give my project name ShMyPdf.

I change the existing class. I change class name to DisplayPdf and it is inherited from WebControl class.


Bookmark and Share

Upload nhiều file trong ASP.NET

Thông qua bài viết này bạn có thể học được cách tạo ra chức năng JavaScript Upload nhiều file ảnh cùng lúc.
JavaScript: Đây là chức năng tải nhiều ảnh lên cùng lúc
<script language="javascript" type="text/javascript">
   function AddMoreImages() {
        if (!document.getElementById && !document.createElement)
            return false;
        var fileUploadarea = document.getElementById("fileUploadarea");
        if (!fileUploadarea)
            return false;
        var newLine = document.createElement("br");
        fileUploadarea.appendChild(newLine);
        var newFile = document.createElement("input");
        newFile.type = "file";
        newFile.setAttribute("class", "fileUpload");

        if (!AddMoreImages.lastAssignedId)
            AddMoreImages.lastAssignedId = 100;
        newFile.setAttribute("id", "FileUpload" + AddMoreImages.lastAssignedId);
        newFile.setAttribute("name", "FileUpload" + AddMoreImages.lastAssignedId);
        var div = document.createElement("div");
        div.appendChild(newFile);
        div.setAttribute("id", "div" + AddMoreImages.lastAssignedId);
        fileUploadarea.appendChild(div);
        AddMoreImages.lastAssignedId++;
    }
</script>
Example: Uploadmultiplefiles.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Uploadmultiplefiles.aspx.cs" Inherits="Uploadmultiplefiles" %>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Multiple Image upload using JavaScript</title>
    <script language="javascript" type="text/javascript">
      
function AddMoreImages() {

           if (!document.getElementById && !document.createElement)
                return false;
            var fileUploadarea = document.getElementById("fileUploadarea");
            if (!fileUploadarea)
                return false;
            var newLine = document.createElement("br");
            fileUploadarea.appendChild(newLine);
            var newFile = document.createElement("input");
            newFile.type = "file";
            newFile.setAttribute("class", "fileUpload");

            if (!AddMoreImages.lastAssignedId)
                AddMoreImages.lastAssignedId = 100;
            newFile.setAttribute("id", "FileUpload" + AddMoreImages.lastAssignedId);
            newFile.setAttribute("name", "FileUpload" + AddMoreImages.lastAssignedId);
            var div = document.createElement("div");
            div.appendChild(newFile);
            div.setAttribute("id", "div" + AddMoreImages.lastAssignedId);
            fileUploadarea.appendChild(div);
            AddMoreImages.lastAssignedId++;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div id="fileUploadarea">
            <asp:FileUpload ID="FileUpload1" runat="server" BorderStyle="Solid" BorderColor="Gray" /><br />
        </div>
        &nbsp;
       <input style="display: block;" id="btnAddMoreImages" type="button" value="Add more images"
onclick="AddMoreImages();" />
        <asp:Button ID="ButtonUpload" runat="server" OnClick="ButtonUpload_Click" Text="Upload" />
    </div>
    </form>
</body>
</html>

 
Uploadmultiplefiles.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

    
protected void ButtonUpload_Click(object sender, EventArgs e)
        {
            HttpFileCollection httpFileCollection = Request.Files;
            for (int i = 0; i < httpFileCollection.Count; i++)
           {
                HttpPostedFile httpPostedFile = httpFileCollection[i];
                if (httpPostedFile.ContentLength > 0)
               {
                    httpPostedFile.SaveAs(Server.MapPath("~/UploadedImages/") + System.IO.Path.GetFileName(httpPostedFile.FileName));    
        }
     }
   }
}
Lưu ý phải tạo thư mục "UploadedImages".

Bookmark and Share

Upload 1 file trong Asp.Net

Bài viết này sẽ chỉ cho chúng ta cách Upload file từ hệ thống bằng việc sử dụng FileUpload Control trong ASP.NET 2.0.

  1. Thêm 1 Form mới vào project.
  2. Đặt tên FileUploading.aspx.
  3. Kéo và thả FileUpload control từ toolbox đến webform.
  4. Kéo và thả Button control từ toolbox đếnwebform. Đặt tên Button Text là "Upload".
  5. Double click lên Button Control đến nó generates sự kiện "Button1_Click" trong file .cs
  6. Và kết thúc bằng việc kéo và thả Label Control từ toolbox đến webform.

Tạo folder trong ổ D với tên "Uploads"


Trong FileUploading.aspx.cs, viết code sau dưới sự kiện "Button1_Click"



Code:
protected void Button1_Click(object sender, EventArgs e)
{
    try
    {
        if (FileUpload1.HasFile)
        {
            FileUpload1.SaveAs(@"D:\Uploads\" + FileUpload1.FileName);
            lblFileMsg.Text = "<b>File Uploaded Successfully.</b><br>";
            lblFileMsg.Text += "File Name :" + FileUpload1.FileName;
            lblFileMsg.Text += "<br>File Type :" + FileUpload1.PostedFile.ContentType;
            lblFileMsg.Text += "<br>File Size :" + FileUpload1.PostedFile.ContentLength;
        }
        else lblFileMsg.Text = "Failed to Upload Your File";
    }
    catch (Exception)
    { }
}
Theo mặc định upload tối đa đến 4MB. Nó không cho phép các tập tin .exe
Để tăng kích thước của tập tin bằng cách thêm 1 thẻ trong web.config. Thêm thẻ này ở giữa.



Code:
<System.Web>
  <httpRuntime executionTimeout="500" maxRequestLength="4096" />
</System.Web>

Sau khi đã upload file, hình dưới thông báo đã thành công:
Click image for larger version

Name:	h1.jpg
Views:	4
Size:	16.0 KB
ID:	2
Nguồn DNGAZ.com - Diễn đàn Đà Nẵng
Bookmark and Share
previous home

Cộng đồng yêu thiết kế Việt Nam Thiet ke website, danang