Re如何实现ASP.NET网站程序的上传文件功能。
有木有童鞋做过。能否上传个救命。
-------------------------
Re如何实现ASP.NET网站程序的上传文件功能。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Aliyun.OpenServices.OpenStorageService;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
string accessId = "你的账号";
string accessKey = "你的密码";
string bucketName = "你的bucket";
protected void Page_Load(object sender, EventArgs e)
{
}
/// <summary>
/// 上传
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Button1_Click(object sender, EventArgs e)
{
//文件名
string key = DateTime.Now.ToString("yyyyMMddHHmmss");
//string fileToUpload = "file.zip";
ObjectMetadata metadata = new ObjectMetadata();
// 可以设定自定义的metadata。
metadata.ContentType = FileUpload1.PostedFile.ContentType;
OssClient ossClient = new OssClient(accessId, accessKey);
using (var fs = FileUpload1.PostedFile.InputStream)
{
key = Path.GetExtension(FileUpload1.PostedFile.FileName);
//目录处理
string ml = string.IsNullOrEmpty(Text1.Text) ? "" : Text1.Text.Trim() "/";
var ret = ossClient.PutObject(bucketName, ml key, fs, metadata);
Label1.Text = ret.ETag;
string url = string.Format("http://{0}.oss.aliyuncs.com/{1}{2}", bucketName, ml, key);
Label2.Text = url;
}
}
//删除
protected void Button2_Click(object sender, EventArgs e)
{
OssClient ossClient = new OssClient(accessId, accessKey);
ossClient.DeleteObject(bucketName, TextBox1.Text);
}
}