請問各位大大
在新增或修改的網頁包含FileUpload
是否一定要有上傳的動作才不會出錯?
若FileUpload不是一定要的欄位(可上傳可不上傳)
該如可處理才不會一直出現
'/' 應用程式中發生伺服器錯誤。
找不到屬性集方法。
描述: 在執行目前 Web 要求的過程中發生未處理的例外狀況。請檢閱堆疊追蹤以取得錯誤的詳細資訊,以及在程式碼中產生的位置。
例外狀況詳細資訊: System.ArgumentException: 找不到屬性集方法。
不知你的平台是啥,php? asp.net ? python ? node.js ? jsp ?
如果是php
if(isset($_POST["file"])){
// 檢查是否有檔案上傳,在這裡再處理檔案後續的動作
}
asp.net
if( Request.Form["file"] != null ){
// 同上
}
其他語言應該大同小異,請自查
python (flask / django)
if request.files.get('file') is not None :
print("有檔案上傳')
#....todo file
平台是c#2019 FineUiPro
若沒按瀏覽上傳檔案時,按保存或離開都正常
但是一但按到瀏覽後就會出現錯誤
請問是什麼原因?
你沒 new Image
請問要在那裡加new lmage
沒提供原始碼,無法判斷
原始碼如下,煩請幫忙看一下,謝謝!
aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="InputDetail2.aspx.cs" Inherits="Job.InputDetail2" %>
</style>
</f:SimpleForm>
</Items>
</f:SimpleForm>
<f:Button ID="btnEdit" runat="server" OnClick="btnEdit_Click"
Text="保存" MarginTop="5px">
</f:Button>
<f:Button ID="btnEsc" runat="server" OnClick="btnEsc_Click"
Text="離開" MarginTop="5px">
</f:Button>
<br />
<f:Label ID="labResult" CssClass="result" EncodeText="false" runat="server">
</f:Label>
<br />
</form>
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.Text;
using FineUIPro;
using System.IO;
namespace Job
{
public partial class InputDetail2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// 启用大字体模式
if (PageManager1.EnableLargeMode ||
PageManager1.DisplayMode == DisplayMode.Large ||
PageManager1.DisplayMode == DisplayMode.LargeSpace)
{
SimpleForm1.LabelWidth = (Unit)120;
}
///
string sId = Context.Request.QueryString["ltId"];
labId.Text = sId;
string sPId = Context.Request.QueryString["ProgramId"];
string sql = string.Format(@"SELECT * FROM ltjob_b WHERE ltId='{0}' AND ProgramId='{1}' ", sId, sPId);
DataTable table = ltDll.ltClass.SelectFromMes(sql);
string aa = "123";
foreach (DataRow dr in table.Rows)
{
labProgramId.Text = dr["ProgramId"].ToString();
ddClass.Text = dr["ltClass"].ToString();
txbPurpose.Text = dr["Purpose"].ToString();
txbPosition.Text = dr["Position"].ToString();
txbNotice.Text = dr["Notice"].ToString();
txbFrequency.Text = dr["Frequency"].ToString();
txbRemarks.Text = dr["Remarks"].ToString();
//txbVideoFile.Text = dr["VideoFile"].ToString();
labFileId.Text = dr["VideoFile"].ToString();
Label1.Text = dr["VideoFile"].ToString();
}
}
}
protected void btnEsc_Click(object sender, EventArgs e)
{
String url = "JobDetail.aspx?ltId=" + labId.Text;
Response.Redirect(url);
}
protected void txbVideoFile_FileSelected(object sender, EventArgs e)
{
if (txbVideoFile.HasFile)
{
string fileName = txbVideoFile.ShortFileName;
// 判斷是否為允許上傳的檔案附檔名
string extension = Path.GetExtension(fileName).ToLowerInvariant();
List<string> allowedExtextsion = new List<string> { ".webm", ".mkv" };
if (allowedExtextsion.IndexOf(extension) == -1)
{
ltDll.ltClass.WebShowMessage("不允許該檔案上傳");
return;
}
// 限制檔案大小,限制為 20MB
int filesize = txbVideoFile.PostedFile.ContentLength;
if (filesize > 21000000) //限制為 20MB
{
ltDll.ltClass.WebShowMessage("檔案大小上限為 20MB,該檔案無法上傳");
return;
}
if (txbVideoFile.HasFile)
{
//string fileName = txbVideoFile.ShortFileName;
fileName = labId.Text + "_" + fileName;
txbVideoFile.Text = fileName;
txbVideoFile.SaveAs(Server.MapPath("~/VideoFile/" + fileName));
}
// 清空文件上传组件(上传后要记着清空,否则点击提交表单时会再次上传!!)
txbVideoFile.Reset();
}
}
protected void btnEdit_Click(object sender, EventArgs e)
{
txbVideoFile.Reset();
string sql = string.Empty;
sql = string.Format(@"UPDATE ltjob_b SET ltClass='{2}', Purpose='{3}',Notice='{4}',Frequency='{5}',
Position='{6}',Remarks='{7}'
WHERE ltId= '{0}' AND ProgramId='{1}'"
, labId.Text
, labProgramId.Text
, ddClass.SelectedValue
, txbPurpose.Text
, txbNotice.Text
, txbFrequency.Text
, txbPosition.Text
, txbRemarks.Text
);
string aa = "123";
ltDll.ltClass.ExecuteToMes(sql);
String url = "JobDetail.aspx?ltId=" + labId.Text;
Response.Redirect(url);
}
}
}
程式碼的範圍稍改一下,另外也提供錯誤訊息的傾印內容
只是想知道是否已有上傳檔案,如:下列給值時就會出現錯誤,若不給值時就沒問題,請問該如何處理,謝謝 !
//fileUpload.Text = "test file";
html
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FileUpload.aspx.cs" Inherits="Job.FileUpload" %>
<Items>
<f:Label runat="server" ID="labId" Label="工號" />
<f:Label runat="server" ID="labPgId" Label="程式代碼" />
<f:FileUpload runat="server" ID="fileUpload" EmptyText="請選擇一個檔案" Label="上傳檔案" ButtonIcon="Add">
</f:FileUpload>
<f:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" ValidateForms="SimpleForm1"
Text="上傳檔案">
</f:Button>
<f:Button ID="btnEsc" runat="server" OnClick="btnEsc_Click" ValidateForms="SimpleForm1"
Text="回上頁">
</f:Button>
</Items>
</f:SimpleForm>
</form>
code
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.Text;
using FineUIPro;
using System.IO;
namespace Job
{
public partial class FileUpload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
labId.Text = Context.Request.QueryString["ltId"];
labPgId.Text = Context.Request.QueryString["ProgramId"];
//fileUpload.Text = "test file";
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
if (fileUpload.HasFile)
{
string fileName = fileUpload.ShortFileName;
// string fileName = txbVideoFile.ShortFileName;
if (fileUpload.HasFile)
{
// 判斷是否為允許上傳的檔案附檔名
string extension = Path.GetExtension(fileName).ToLowerInvariant();
List<string> allowedExtextsion = new List<string> { ".webm", ".mkv" };
if (allowedExtextsion.IndexOf(extension) == -1)
{
ltDll.ltClass.WebShowMessage("不允許該檔案上傳");
return;
}
// 限制檔案大小,限制為 20MB
int filesize = fileUpload.PostedFile.ContentLength;
if (filesize > 21000000) //限制為 20MB
{
ltDll.ltClass.WebShowMessage("檔案大小上限為 20MB,該檔案無法上傳");
return;
}
//string fileName = txbVideoFile.ShortFileName;
fileName = labId.Text + "_" + fileName;
fileUpload.Text = fileName;
fileUpload.SaveAs(Server.MapPath("~/VideoFile/" + fileName));
//labResult.Text = "<p>文件路径:" + filePhoto.FileName + "</p>" +
// "<p>程式代碼:111</p>" +
// "<p>头像:<br /><img src=\"";// + PageContext.ResolveUrl("~/upload/" + fileName) + "\" /></p>";
// 清空表单字段(第三种方法)
SimpleForm1.Reset();
string sql = string.Empty;
sql = string.Format(@"UPDATE ltjob_b SET VideoFile='{2}'
WHERE ltId= '{0}' AND ProgramId='{1}'"
, labId.Text
, labPgId.Text
, fileName
);
string aa = "123";
ltDll.ltClass.ExecuteToMes(sql);
}
String url = "JobDetail.aspx?ltId=" + labId.Text;
Response.Redirect(url);
}
}
protected void btnEsc_Click(object sender, EventArgs e)
{
String url = "JobDetail.aspx?ltId=" + labId.Text;
Response.Redirect(url);
}
}
}
FileUpload元件沒有Text屬性啦.......放個label來放你要提示的字串
感謝大師!