|
|
|
ทะไม่ลบไฟล์ แล้วมันขึ้นแบบนี้อ่ะครับ พอเปิดโปรแกรม ผมก็ดึงรูปมาแสดงที่ picture box และพอเลือกไฟล์ แล้วพอกด save จะให้มันลบรูปเก่าออก |
|
|
|
|
|
|
|
เจ้าสิต้อง close file ก่อนเด้อ
|
|
|
|
|
Date :
2010-05-21 12:05:38 |
By :
tungman |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
close ยังไงอ่ะ
ใช้ file.exit หรือ Image.Dispose() กะลบมะได้อยู่ดี
|
|
|
|
|
Date :
2010-05-21 12:10:46 |
By :
teemsu |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
อ่านไฟล์ขึ้น memory stream ก่อน
แล้วปิดไฟล์ต้นฉบับ
เอา memory stream มาแสดง
ทีนี้จะทำอะไรก็ได้แล้วค่ะ
|
|
|
|
|
Date :
2010-05-21 15:29:46 |
By :
blurEyes |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
อืมๆ แบบนั้นแหละ แล้วก็ Dispose ด้วยอย่าลืม
กำลังเขียนตัวอย่างมาอยู่เชียว โดนตัดหน้าซะแล้ว อิอิ
ตัวอย่าง upload file excel เพื่ออ่าน อ่านแล้วเก็บใส่ datatable ซึ่งจะเก็บ data ใน excel ไว้ใน memory
จากนั้นค่อยลบ excel ทิ้ง
ReadExcel.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ReadExcel.aspx.cs" Inherits="ReadExcel" %>
<!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 runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="ReadButton" runat="server" Text="Read" />
<br />
<br />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
ReadExcel.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Text;
public partial class ReadExcel : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
FilterExtension();
FileUpload1.Attributes.Add("onchange", string.Format("javascript:return checkFileExtension(this, '{0}');", ReadButton.ClientID));
ReadButton.Enabled = false;
ReadButton.Click += new EventHandler(ReadButton_Click);
}
protected void ReadButton_Click(object sender, EventArgs e)
{
string filePath = UploadFile();
if (filePath != string.Empty)
{
GridView1.DataSource = ReadFile(filePath);
GridView1.DataBind();
DeleteFile(filePath);
}
}
private string UploadFile()
{
string savePath = Request.PhysicalApplicationPath + "\\";
string postedFilePath = string.Empty;
if (FileUpload1.HasFile)
{
try
{
string filePath = savePath + FileUpload1.FileName;
FileUpload1.SaveAs(filePath);
postedFilePath = savePath + FileUpload1.PostedFile.FileName;
}
catch (Exception ex)
{
MessageBox.Show("ไม่สามารถอัฟโหลดได้ จากสาเหตุ: " + ex.Message);
}
}
return postedFilePath;
}
private DataTable ReadFile(string filePath)
{
DataTable Dt = new DataTable();
string oleDbConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";", filePath);
OleDbConnection oleDbConnection = new OleDbConnection(oleDbConnectionString);
DataRow Dr = ReadSheet(oleDbConnection).Rows[0];
string oleDbCoomandString = string.Format("Select * From [{0}]", Dr["TABLE_NAME"].ToString());
OleDbCommand oleDbCommand = new OleDbCommand(oleDbCoomandString, oleDbConnection);
OleDbDataAdapter dataAdapter = new OleDbDataAdapter();
dataAdapter.SelectCommand = oleDbCommand;
try
{
dataAdapter.Fill(Dt);
}
catch (Exception ex)
{
MessageBox.Show("ไม่สามารถอ่านได้ จากสาเหตุ: " + ex.Message);
}
dataAdapter.Dispose();
oleDbCommand.Dispose();
oleDbConnection.Dispose();
return Dt;
}
private bool DeleteFile(string filePath)
{
FileInfo excelFile = new FileInfo(filePath);
bool success = false;
try
{
excelFile.Delete();
success = true;
}
catch (Exception ex)
{
MessageBox.Show("ไม่สามารถลบได้ จากสาเหตุ: " + ex.Message);
}
return success;
}
private DataTable ReadSheet(OleDbConnection oleDbConnection)
{
DataTable Dt = new DataTable();
try
{
oleDbConnection.Open();
Dt = oleDbConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
oleDbConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show("ไม่สามารถหาชีตได้ จากสาเหตุ: " + ex.Message);
}
return Dt;
}
private void FilterExtension()
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendLine("function checkFileExtension(elem, btn)");
stringBuilder.AppendLine("{");
stringBuilder.AppendLine("\tvar filePath = elem.value;");
stringBuilder.AppendLine("\tvar button = document.getElementById(btn);\n");
stringBuilder.AppendLine("\tif(filePath.indexOf('.') == -1)");
stringBuilder.AppendLine("\t{");
stringBuilder.AppendLine("\t\tbutton.disabled = false;");
stringBuilder.AppendLine("\t\treturn false;");
stringBuilder.AppendLine("\t}\n");
stringBuilder.AppendLine("\tvar validExtensions = new Array();");
stringBuilder.AppendLine("\tvar ext = filePath.substring(filePath.lastIndexOf('.') + 1).toLowerCase();\n");
stringBuilder.AppendLine("\tvalidExtensions[0] = 'xls';");
stringBuilder.AppendLine("\t//validExtensions[1] = 'doc';");
stringBuilder.AppendLine("\t//validExtensions[2] = 'bmp';");
stringBuilder.AppendLine("\t//validExtensions[3] = 'png';");
stringBuilder.AppendLine("\t//validExtensions[4] = 'gif';");
stringBuilder.AppendLine("\t//validExtensions[5] = 'tif';");
stringBuilder.AppendLine("\t//validExtensions[6] = 'tiff';");
stringBuilder.AppendLine("\t//validExtensions[7] = 'txt';");
stringBuilder.AppendLine("\t//validExtensions[8] = 'jpg';");
stringBuilder.AppendLine("\t//validExtensions[9] = 'jpeg';");
stringBuilder.AppendLine("\t//validExtensions[10] = 'pdf';\n");
stringBuilder.AppendLine("\tfor(var i = 0; i < validExtensions.length; i++)");
stringBuilder.AppendLine("\t{");
stringBuilder.AppendLine("\t\tif(ext == validExtensions[i])");
stringBuilder.AppendLine("\t\t{");
stringBuilder.AppendLine("\t\t\tbutton.disabled = false;");
stringBuilder.AppendLine("\t\t\treturn true;");
stringBuilder.AppendLine("\t\t}");
stringBuilder.AppendLine("\t}\n");
stringBuilder.AppendLine("\talert('The file extension ' + ext.toUpperCase() + ' is not allowed!');");
stringBuilder.AppendLine("\tbutton.disabled = true;");
stringBuilder.AppendLine("\treturn false;");
stringBuilder.AppendLine("}");
ClientScriptManager ClientScript = Page.ClientScript;
if (!ClientScript.IsClientScriptBlockRegistered(this.GetType(), "JavaScript_Change"))
ClientScript.RegisterClientScriptBlock(this.GetType(), "JavaScript_Change", stringBuilder.ToString(), true);
}
}
|
|
|
|
|
Date :
2010-05-21 15:48:07 |
By :
tungman |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณทุคนครับ
เด๋วลองทำดูครับ
|
|
|
|
|
Date :
2010-05-22 15:59:56 |
By :
teemsu |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
สมมติว่า ผมเก็บข้อมูลรูปภาพเป็นแบบ url ประมาณนี้อ่ะครับ
c:\upload\picture_1.jpg
จะทำไงหรอ ให้ memory stream ได้อ่ะ
|
|
|
|
|
Date :
2010-05-22 16:39:05 |
By :
teemsu |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (C#)
FileStream imageStream = new FileStream(@"c:\upload\picture_1.jpg", FileMode.Open, FileAccess.Read);
byte[] byteArray = new byte[imageStream.Length];
imageStream.Read(byteArray, 0, byteArray.Length);
BitmapImage myBitmapImage = new BitmapImage();
myBitmapImage.BeginInit();
myBitmapImage.StreamSource = new MemoryStream(byteArray);
myBitmapImage.EndInit();
Image myImage = new Image();
myImage.Source = myBitmapImage;
|
|
|
|
|
Date :
2010-05-22 19:08:49 |
By :
tungman |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณครับ
|
|
|
|
|
Date :
2010-05-22 19:30:05 |
By :
teemsu |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|