百度webupload上傳 1

來源:互聯網
上載者:User

標籤:

百度webupload網址:http://fex.baidu.com/webuploader/

引入js和css

<script src="../../Content/webuploader.js"></script>
<link href="../../Content/webuploader.css" rel="stylesheet" />

頁面html代碼

<div id="uploader" class="wu-example">
<!--用來存放檔案資訊-->
<div id="thelist" class="uploader-list"></div>
<div class="btns">
<div id="picker">選擇檔案</div>
<button id="ctlBtn" class="btn btn-default">開始上傳</button>
</div>
</div>

初始化webupload.js

<script type="text/javascript">
// 檔案上傳
jQuery(function () {
var $ = jQuery,
$list = $(‘#thelist‘),
$btn = $(‘#ctlBtn‘),
state = ‘pending‘,
uploader;

uploader = WebUploader.create({

// 不壓縮image
resize: false,

// swf檔案路徑
swf: ‘/content/Uploader.swf‘,

// 檔案接收服務端。
server: ‘/Webupload/Process‘,   (這是在後台寫的接收前台傳送的檔案方法)

// 選擇檔案的按鈕。可選。
// 內部根據當前運行是建立,可能是input元素,也可能是flash.
pick: ‘#picker‘,


});

// 當有檔案添加進來的時候
uploader.on(‘fileQueued‘, function (file) {
$list.append(‘<div id="‘ + file.id + ‘" class="item">‘ +
‘<h4 class="info">‘ + file.name + ‘</h4>‘ +
‘<p class="state">等待上傳...</p>‘ +
‘</div>‘);
});

// 檔案上傳過程中建立進度條即時顯示。
uploader.on(‘uploadProgress‘, function (file, percentage) {
var $li = $(‘#‘ + file.id),
$percent = $li.find(‘.progress .progress-bar‘);

// 避免重複建立
if (!$percent.length) {
$percent = $(‘<div class="progress progress-striped active">‘ +
‘<div class="progress-bar" role="progressbar" style="width: 0%">‘ +
‘</div>‘ +
‘</div>‘).appendTo($li).find(‘.progress-bar‘);
}

$li.find(‘p.state‘).text(‘上傳中‘);

$percent.css(‘width‘, percentage * 100 + ‘%‘);
});

uploader.on(‘uploadSuccess‘, function (file) {
$(‘#‘ + file.id).find(‘p.state‘).text(‘已上傳‘);
});

uploader.on(‘uploadError‘, function (file) {
$(‘#‘ + file.id).find(‘p.state‘).text(‘上傳出錯‘);
});

uploader.on(‘uploadComplete‘, function (file) {
$(‘#‘ + file.id).find(‘.progress‘).fadeOut();
});

uploader.on(‘all‘, function (type) {
if (type === ‘startUpload‘) {
state = ‘uploading‘;
} else if (type === ‘stopUpload‘) {
state = ‘paused‘;
} else if (type === ‘uploadFinished‘) {
state = ‘done‘;
}

if (state === ‘uploading‘) {
$btn.text(‘暫停上傳‘);
} else {
$btn.text(‘開始上傳‘);
}
});

$btn.on(‘click‘, function () {
if (state === ‘uploading‘) {
uploader.stop();
} else {
uploader.upload();
}
});
});

</script>

後台接收上傳的檔案的代碼(接收介面)

public class WebuploadController : Controller
{
//
// GET: /Webupload/

public WebuploadController()
{
var applicationPath = VirtualPathUtility.ToAbsolute("~") == "/" ? "" : VirtualPathUtility.ToAbsolute("~");
urlPath = "/PHP";//儲存的地址
}

static string urlPath = string.Empty;
public ActionResult Process(string id, string name, string type, string lastModifiedDate, int size, HttpPostedFileBase file)
{
string filePathName = string.Empty;

string localPath = Path.Combine(HttpRuntime.AppDomainAppPath, "PHP");
if (Request.Files.Count == 0)
{
return Json(new { jsonrpc = 2.0, error = new { code = 102, message = "儲存失敗" }, id = "id" });
}

string ex = Path.GetExtension(file.FileName);
filePathName = Guid.NewGuid().ToString("N") + ex;
if (!System.IO.Directory.Exists(localPath))
{
System.IO.Directory.CreateDirectory(localPath);
}
file.SaveAs(Path.Combine(localPath, filePathName));

return Json(new
{
jsonrpc = "2.0",
id = "id",
filePath = urlPath + "/" + filePathName
});
}

}

對於上傳大的檔案需要在設定檔中改iis配置

<system.web>

<httpRuntime maxRequestLength="999999999" />//使用者上傳檔案最大體積
<compilation debug="true" targetFramework="4.0" />

</system.web>

 

<system.webServer>

<security>

<requestFiltering>
<requestLimits maxAllowedContentLength="3000000000" />
</requestFiltering>
</security>
</system.webServer>

 

百度webupload上傳 1

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.