下面是一個上傳類的在C#腳本的一部分。我是一名PHP程序員,我從未與C#搞混,但我正在努力學習。這個上傳腳本不會處理除圖像以外的任何內容,我需要修改這個類來處理其他類型的媒體,或者一起重寫。如果我是正確的,我認識到,c#視頻相當於image.fromstream?或改變以下腳本的範圍,允許視頻太
using (Image image = Image.FromStream(file.InputStream))
基本上說,下面的範圍是圖片,只能使用一個圖像或對象被丟棄?也正被從文件流的圖片,我的理解是,像... PHP中的$ _FILES數組創建可變圖像?
我不知道,我現在不是真的在乎製作縮略圖,所以如果這個可以取出並仍然處理上傳,我完全很酷,我只是沒有得到任何運氣這個東西拿任何東西,但圖像,註釋掉類是整體的一部分,即使...
protected void Page_Load(object sender, EventArgs e)
{
string dir = Path.Combine(Request.PhysicalApplicationPath, "files");
if (Request.Files.Count == 0)
{
// No files were posted
Response.StatusCode = 500;
}
else
{
try
{
// Only one file at a time is posted
HttpPostedFile file = Request.Files[0];
// Size limit 100MB
if (file.ContentLength > 102400000)
{
// File too large
Response.StatusCode = 500;
}
else
{
string id = Request.QueryString["userId"];
string[] folders = userDir(id);
foreach (string folder in folders)
{
dir = Path.Combine(dir, folder);
if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
}
string path = Path.Combine(dir, String.Concat(Request.QueryString["batchId"], "_", file.FileName));
file.SaveAs(path);
// Create thumbnail
int dot = path.LastIndexOf('.');
string thumbpath = String.Concat(path.Substring(0, dot), "_thumb", path.Substring(dot));
using (Image image = Image.FromStream(file.InputStream))
{
// Find the ratio that will create maximum height or width of 100px.
double ratio = Math.Max(image.Width/100.0, image.Height/100.0);
using (Image thumb = new Bitmap(image, new Size((int)Math.Round(image.Width/ratio), (int)Math.Round(image.Height/ratio))))
{
using (Graphics graphic = Graphics.FromImage(thumb))
{
// Make sure thumbnail is not crappy
graphic.SmoothingMode = SmoothingMode.HighQuality;
graphic.InterpolationMode = InterpolationMode.High;
graphic.CompositingQuality = CompositingQuality.HighQuality;
// JPEG
ImageCodecInfo codec = ImageCodecInfo.GetImageEncoders()[1];
// 90% quality
EncoderParameters encode = new EncoderParameters(1);
encode.Param[0] = new EncoderParameter(Encoder.Quality, 90L);
// Resize
graphic.DrawImage(image, new Rectangle(0, 0, thumb.Width, thumb.Height));
// Save
thumb.Save(thumbpath, codec, encode);
}
}
}
// Success
Response.StatusCode = 200;
}
}
catch
{
// Something went wrong
Response.StatusCode = 500;
}
}
}
,如果你只是想將文件保存到服務器上,它似乎你已經與做「file.SaveAs(路徑);」。如果這是目標,那麼就把其他的東西和你的套裝撕掉。 這完全是我想太多,但它似乎並沒有接受任何不是一個圖像,所以我猜我的問題出在其它地方..謝謝 – Daniel 2010-05-01 17:30:11