我现在要在本地传图片至服务器
不限制图片大小 我现在用FileShow1.PostedFile.SaveAs();这种方法的极限是3M 超过3M是传不上去而且传上去也要一分钟以上有没有其它的方法传图片,不管什么方法都行,只要速度能快一些就可以了哪位大哥有这方面的源码发我一下.谢谢了
不限制图片大小 我现在用FileShow1.PostedFile.SaveAs();这种方法的极限是3M 超过3M是传不上去而且传上去也要一分钟以上有没有其它的方法传图片,不管什么方法都行,只要速度能快一些就可以了哪位大哥有这方面的源码发我一下.谢谢了
web上传限制可以在web.config中设置,
在<system.web>中加入
<httpRuntime maxRequestLength="10240" />
当然,要上传越大的文件(如几百M)就占用更多的内存,如果服务器内存太小的话,一样会出现错误
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Collections;namespace fileUpd
{
/// <summary>
/// .Net通用大文件上传类(可上传大文件,vs2005)
/// 作者:启程 www.letwego.cn
/// 保留作者信息,可用于任意用途
/// </summary>
public class UploadFile
{
private string _filePath;//文件存放路径
private string _htmlKey;//input file name
private bool _imgSlt;
private string _filePathSlt;
private string _imgSltMod;
private int _imgSltWidth;
private int _imgSltHeight;
private ArrayList _result = new ArrayList(); public UploadFile()
{
ImgSlt = false;
ImgSltMod = "HW";
ImgSltWidth = 190;
ImgSltHeight = 135;
}
/// <summary>
/// 文件存放地址
/// </summary>
public string FilePath
{
set { _filePath = value; }
get { return _filePath; }
}
/// <summary>
/// file key 标识
/// </summary>
public string HtmlKey
{
set { _htmlKey = value; }
get { return _htmlKey; }
}
/// <summary>
/// 是否启用缩略图
/// </summary>
public bool ImgSlt {
set { _imgSlt = value; }
get { return _imgSlt; }
}
/// <summary>
/// 缩略图存放位置
/// </summary>
public string FilePathSlt
{
set { _filePathSlt = value; }
get { return _filePathSlt; }
}
/// <summary>
/// 缩略图剪切模式
/// </summary>
public string ImgSltMod
{
set { _imgSltMod = value; }
get { return _imgSltMod; }
}
/// <summary>
/// 缩略图宽
/// </summary>
public int ImgSltWidth
{
set { _imgSltWidth = value; }
get { return _imgSltWidth; }
}
/// <summary>
/// 缩略图高
/// </summary>
public int ImgSltHeight
{
set { _imgSltHeight = value; }
get { return _imgSltHeight; }
}
/// <summary>
/// 返回结果
/// </summary>
public ArrayList Result {
set { _result.Add(value); }
get { return _result; }
}
/// <summary>
/// 上传图片
/// </summary>
/// <param name="filePath">图片放置路径</param>
/// <param name="htmlKey">input file 的名字</param>
/// <returns></returns>
public void ExecUploadFile(HttpFileCollection fileCollection)
{
String nowTime = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss").Replace("-", "").Replace(":", "").Replace(" ","");
ArrayList arrayFilePath = new ArrayList();
for (int i = 0; i < fileCollection.Count; i++)
{
if (fileCollection.Keys[i] == _htmlKey)
{
//没有选取文件
if (fileCollection[i].ContentLength == 0) {
arrayFilePath.Add("1");
Result.Add(arrayFilePath);
}
else
{
string fileContentType = fileCollection[i].ContentType;
string filea = fileCollection[i].FileName;
string fileName = nowTime + i.ToString() + filea.Substring(filea.LastIndexOf(@"\") + 1); // 文件名称
string fileName_s = "x_" + fileName; // 缩略图文件名称
string fileName_sy = "text_" + fileName; // 水印图文件名称(文字)
string fileName_syp = "water_" + fileName; // 水印图文件名称(图片)
string webPath = _filePath+fileName;
string webFilePath = System.Web.HttpContext.Current.Server.MapPath(webPath);
//string webFilePath = _filePath + fileName; // 服务器端文件路径
string webFilePath_slt = System.Web.HttpContext.Current.Server.MapPath(_filePathSlt) + fileName; // 服务器端缩略图文件路径
string webFilePath_syp = ""; // 服务器端带水印图路径(图片)
string webFilePath_sypf = ""; // 服务器端水印图路径(图片)
int fileSize = fileCollection[i].ContentLength; string fileOkType = "image/bmp|image/gif|image/pjpeg|image/jpg|image/jpeg|application/x-shockwave-flash"; if (fileOkType.IndexOf(fileContentType) > -1)
{
if (!File.Exists(webFilePath))
{
try
{
//上传图片
fileCollection[i].SaveAs(webFilePath); //正常保存
//AddWaterPic(webFilePath, webFilePath_syp, webFilePath_sypf); //加上水印
if (!(_imgSlt == false))
{
MakeThumbnail(webFilePath, webFilePath_slt, _imgSltWidth, _imgSltHeight, _imgSltMod, "JPG");
}
arrayFilePath.Add(webPath.Replace(@"\","/"));
Result.Add(arrayFilePath);
}
catch (Exception exc)
{
arrayFilePath.Add(exc.Message);
Result.Add(arrayFilePath);
}
}
//处理问存在问题,重名问题:2
else
{
arrayFilePath.Add("2");
Result.Add(arrayFilePath);
}
}
//图片格式问题:3
else
{
arrayFilePath.Add("3");
Result.Add(arrayFilePath);
}
}
}
}
}
/// <summary>
/// 生成缩略图
/// </summary>
/// <param name="originalImagePath">源图路径(物理路径)</param>
/// <param name="thumbnailPath">缩略图路径(物理路径)</param>
/// <param name="width">缩略图宽度</param>
/// <param name="height">缩略图高度</param>
/// <param name="mode">生成缩略图的方式</param>
public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode, string type)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath); int towidth = width;
int toheight = height; int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height; switch (mode)
{
case "HW"://指定高宽缩放(可能变形)
break;
case "W"://指定宽,高按比例
toheight = originalImage.Height * width / originalImage.Width;
break;
case "H"://指定高,宽按比例
towidth = originalImage.Width * height / originalImage.Height;
break;
case "Cut"://指定高宽裁减(不变形)
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * height / towidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}
break;
case "DB"://等比缩放(不变形,如果高大按高,宽大按宽缩放)
if ((double)originalImage.Width / (double)towidth < (double)originalImage.Height / (double)toheight)
{
toheight = height;
towidth = originalImage.Width * height / originalImage.Height;
}
else
{
towidth = width;
toheight = originalImage.Height * width / originalImage.Width;
}
break;
default:
break;
} //新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); //新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); //设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空画布并以透明背景色填充
g.Clear(System.Drawing.Color.Transparent); //在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
new System.Drawing.Rectangle(x, y, ow, oh),
System.Drawing.GraphicsUnit.Pixel); try
{
//保存缩略图
if (type == "JPG")
{
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
if (type == "BMP")
{
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Bmp);
}
if (type == "GIF")
{
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Gif);
}
if (type == "PNG")
{
bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Png);
}
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
}
}
}
我用的!还可以吧!
//上传图片
fileCollection[i].SaveAs(webFilePath);
这和我说的上传大图片会导致页面丢失还是没有解决 只是多了一个上传缩略图,又有什么用,我还是得上传原图
客服端 SOCKET 图片 出去
你说的FTP是啥?
我是说用FTP直接把大的东西传进空间
用FTP最稳定 一般大文件都直接用FTP传到空间