我有 2 頁。一頁發送一個 ID 號,稍后我將使用它從資料庫上傳資料。
我通過 url - 發送這個 ID 號location = "fileUploadPage.aspx?" ID"。然后我需要在該頁面的服務器端上傳檔案。我需要表單不重新加載頁面,因為它正在洗掉我的 URL 擴展名。我考慮過使用 sessionStorage - 但我覺得在我的情況下更好,因為用戶可以為不同的專案打開多個選項卡以將檔案上傳到。
將檔案上傳到服務器端后 - 我還需要將其轉換為 PDF。這幾天我一直在嘗試這樣做,但我無法解決它。
我設法將檔案從表單上傳到服務器端檔案夾,但我無法拒絕重新加載頁面。當我拒絕重新加載頁面時,服務器端功能沒有執行。另外,我無法轉換為PDF。
我在服務器端使用 aspx.net c#。遺憾的是,我無法分享原始代碼,因為它位于封閉的地方,但我在本地電腦上做了一個演示:
有什么建議?我是處理檔案領域的新手——以前從未這樣做過。任何關于重構我的代碼或我如何移動 ID 的建議都非常受歡迎。
輸入的數字也是我在將其轉換為 PDF 后需要添加到我的檔案名中的文本。
<form id="myForm" name="myForm" action="FilesProblemPage.aspx" runat="server" style="margin-top: 20px;">
<select id="Number" runat="server">
<option value="3">333333333</option>
<option value="2">222222222</option>
</select>
<label runat="server">
click me to choose a file
<input id="uploadFile" name="uploadFile" style="visibility: hidden" type="file" runat="server" />
</label>
<p id="ChosenFile">no file selected</p>
<asp:Button ID="uploadButton" runat="server" Text="Upload" type="button"
OnClick="uploadButton_Click" BorderStyle="None" CssClass="button" />
</form>
let makat = location.href.split("?")[1];
if (makat == 44459999) {
$("#makat").val("workssss");
$(".checkingTemp")[0].checked = true;
$(".checkingTemp")[1].checked = true;
}
$("#body_uploadFile")[0].addEventListener("change", function (e) {
console.log($("#body_uploadFile")[0].files);
if ($("#body_uploadFile")[0].files[0] != undefined)
$("#ChosenFile").text($("#body_uploadFile")[0].files[0].name);
else
$("#ChosenFile").text("no file chosen");
})
服務器端:添加:
using System.IO;
protected void uploadButton_Click(object sender, EventArgs e)
{
if (uploadFile.PostedFile != null && uploadFile.PostedFile.ContentLength > 0)
{
string fileName = Path.GetFileName(uploadFile.PostedFile.FileName);
string folder = Server.MapPath("~/TempFiles/");
Directory.CreateDirectory(folder);
uploadFile.PostedFile.SaveAs(Path.Combine(folder, fileName));
try
{
Response.Write("<script>alert('operation success')</script>");
}
catch
{
Response.Write("<script>alert('operation failed')</script>");
}
}
}
uj5u.com熱心網友回復:
好吧,您仍然可以使用 session() 來傳遞 ID,但是在第一頁加載時(在下一頁上,您將該 ID 保存到 ViewState 中。這樣,他們是否打開了多個頁面都無關緊要,因為他們跳轉到下一頁,然后在第一頁加載 IsPostBack = false,然后轉移到 ViewState。
ViewState 是每個網頁的,因為 session() 是全域的。因此,通過會話傳遞 id,下一頁的第一件事是將值傳遞給 ViewState。
但是,僅使用簡單的 FileUpLoad 控制元件的問題在于它們并不是那么好,如果檔案更大,那么在上傳程序中您不會獲得任何進展。
出于這個原因,我傾向于在檔案上傳上花費一些真正的努力。(因為這對開發人員來說是一種痛苦,而且通常對用戶來說也是如此)。這方面有很多選擇,但我在我的專案中使用了 AjaxToolKit,因此采用了那個。
因此,用戶可以拖放檔案,或選擇多個檔案,然后點擊上傳按鈕。
因此,AjaxToolKit 上傳器看起來像這樣:

因此,用戶可以選擇一堆檔案 - 洗掉它們,做任何事情。
然后他們可以點擊上傳按鈕。
每個檔案上傳 - 帶有進度條。然后在上傳后,我會顯示上傳的檔案。
例如這個:

上傳器的另一個優點是沒有真正的檔案大小限制 - 它以小塊上傳。
So, it really depends on how fancy you want to get, but there are quite a few "up-loader" examples and even some jquery JavaScript ones that are quite nice.
As suggested, if you not using the AjaxControl toolkit, then you could consider it (it a bit over kill - but the toolkit does have a lot of other nice features).
As noted, you might want to better use at least a asp.net FileUpload control, but it depends on how many files, how large, and what kind of UI your looking for here?
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/453544.html
