asp.net多圖片上傳同時儲存對每張圖片的描述

來源:互聯網
上載者:User

標籤:

 

前台aspx

//圖片預覽和描述
function previewImage(file) {
var div = document.getElementById(‘preview‘);
div.innerHTML = "";
for (var i = 0; i < file.files.length; i++) {
//alert(file.files[i]);
var ndiv = document.createElement("div");
ndiv.style.height = "150px";
ndiv.style.width = "300px";
ndiv.style.cssFloat = "left";
var img = document.createElement("img");
var textArea = document.createElement("textarea");
textArea.style.width = "100";
textArea.style.height = "60px";
textArea.setAttribute("name", "note"+i);//給填寫備忘的控制項一個名字,和圖片數量關聯
ndiv.appendChild(img);
ndiv.appendChild(textArea);
img.id = "img" + i;
div.appendChild(ndiv);
img.width = 200;
img.height = 200;

}
for (var i = 0; i < file.files.length; i++) {
var pic = document.getElementById("img" + i);
pic.src = window.URL.createObjectURL(file.files[i]);

}

}

<form id="form1" method="post" runat="server" enctype="multipart/form-data">
<div style="text-align: center">
<div>
<h3>車輛圖片上傳</h3>
<input type="file" multiple="multiple" onchange="previewImage(this)" id="myimage" runat="server" class="btn btn-default" style="margin-left: 40%; height: 69px; width: 280px" />
<p id="MyFile">
<asp:Button runat="server" Text="確認返回" OnClick="Unnamed_Click" CssClass="btn btn-default" Height="68px" Width="124px" />
&nbsp;&nbsp;
<asp:Button runat="server" Text="開始上傳" ID="UploadButton" OnClick="UploadButton_Click" CssClass="btn btn-default" Height="69px" Width="129px"></asp:Button>
</p>
<p>
<asp:Label ID="strStatus" runat="server" Font-Names="宋體" Font-Bold="True" Font-Size="9pt" Width="500px"
BorderStyle="None" BorderColor="White"></asp:Label>
</p>
</div>

<%-- 圖片預覽 --%>
<div id="preview">
</div>
</div>
</form>

後台cs

private void SaveImages()
{

///遍曆File表單元素
HttpFileCollection files = HttpContext.Current.Request.Files;

/// 狀態資訊
System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
strMsg.Append("上傳的檔案分別是:<hr color=red>");
try
{
for (int iFile = 0; iFile < files.Count; iFile++)
{
string note= Request["note"+iFile];//擷取每張圖片的描述內容
string fileName = files[iFile].FileName;
string path = Server.MapPath("~/upload/");
string imgType = files[iFile].ContentType.ToString(); // 圖片類型

if (fileName != "" && (imgType.Equals("image/bmp") || imgType.Equals("image/jpg") || imgType.Equals("image/jpeg") || imgType.Equals("image/gif") || imgType.Equals("image/png")))
{

string upPath = FileController.GetUpPath();
fileName = iFile + "_" + fileName;
string path2 = Server.MapPath("~/upload/");
//產生原圖
Byte[] oFileByte = new byte[files[iFile].ContentLength];
System.IO.Stream oStream = files[iFile].InputStream;
System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream);

int oWidth = oImage.Width; //原圖寬度
int oHeight = oImage.Height; //原圖高度

int tWidth = 100; //設定縮圖初始寬度
int tHeight = 100; //設定縮圖初始高度

int add1 = 20;
int add2 = 120;

Graphics g = null;
Bitmap tImage = null;

string fileExtension = System.IO.Path.GetExtension(fileName);
strMsg.Append("上傳的檔案類型:" + files[iFile].ContentType.ToString() + "<br>");
strMsg.Append("用戶端檔案地址:" + files[iFile].FileName + "<br>");
strMsg.Append("上傳檔案的檔案名稱:" + fileName + "<br>");
strMsg.Append("上傳檔案的副檔名:" + fileExtension + "<br><hr>");
for (int i = 0; i < 4; i++)
{
//按比例計算出縮圖的寬度和高度
if (oWidth >= oHeight)
{
tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oWidth)));
}
else
{
tWidth = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(tHeight) / Convert.ToDouble(oHeight)));
}
//產生縮減原圖
tImage = new Bitmap(tWidth, tHeight);
g = Graphics.FromImage(tImage);
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //設定高品質插值法
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//設定高品質,低速度呈現平滑程度
g.Clear(Color.Transparent); //清空畫布並以透明背景色填充
g.DrawImage(oImage, new Rectangle(0, 0, tWidth, tHeight), new Rectangle(0, 0, oWidth, oHeight), GraphicsUnit.Pixel);
string savePath2 = path + files[iFile].FileName;
string savePath = "~/upload/" + "(" + tWidth + "-" + tHeight + ")" + fileName;
try
{
//以JPG格式儲存圖片
//oImage.Save(oFullName, System.Drawing.Imaging.ImageFormat.Jpeg);
tImage.Save(path2 + "(" + tWidth +"-"+ tHeight + ")" + fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
//儲存圖片路徑到資料庫
inv_worklistInfo inv_wlInfo = new inv_worklistInfo();
inv_wlInfo.picurl = savePath;
inv_wlInfo.workid = workid;
inv_wlInfo.notes = note;
inv_worklistFactory.Create().Add(inv_wlInfo);
}
catch (Exception ex)
{
strStatus.Text = ex.Message;
}
if (i == 0)
{
tWidth = 100 + add1; //設定縮圖初始寬度
tHeight = 100 + add1; //設定縮圖初始高度
}
if (i == 1)
{
tWidth = 100 + add2; //設定縮圖初始寬度
tHeight = 100 + add2; //設定縮圖初始高度
}
if (i == 2)
{
tWidth = oWidth; //設定縮圖初始寬度
tHeight = oHeight; //設定縮圖初始高度
}
}
////釋放資源
if (!CmpUtil.IsNullOEmp(oImage))
{
oImage.Dispose();
}
if (!CmpUtil.IsNullOEmp(g))
{
g.Dispose();
}
if (!CmpUtil.IsNullOEmp(tImage))
{
tImage.Dispose();
}
}
else
{
JSController.Alert(this, "圖片格式只支援:jpeg,jpg,png,bmp,gif");
}
}
strStatus.Text = strMsg.ToString();
}
catch (System.Exception Ex)
{
strStatus.Text = Ex.Message;
}
}

}

asp.net多圖片上傳同時儲存對每張圖片的描述

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.