मैं "असंभव" के साथ सही उत्तर के रूप में चिह्नित होने में सहमत होने में असफल रहा। यदि कोई अभी भी किसी संभावना की तलाश में है, तो यहां मेरे आसपास काम करने वाला काम है। मैं एमवीसी 5 का उपयोग कर रहा हूँ। विचार एक सत्र चर का उपयोग करना है। मुझे ASP.Net Form से विचार मिला।
मेरे मॉडल/ViewModel (केवल प्रासंगिक गुण):
public partial class emp_leaves
{
public string fileNameOrig { get; set; }
public byte[] fileContent { get; set; }
public HttpPostedFileBase uploadFile { get; set; }
}
मेरी नियंत्रक में (HttpPost): // चेक
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(emp_leaves emp_leaves)
{
if (emp_leaves.uploadFile != null && emp_leaves.uploadFile.ContentLength>0 && !string.IsNullOrEmpty(emp_leaves.uploadFile.FileName))
{
emp_leaves.fileNameOrig = Path.GetFileName(emp_leaves.uploadFile.FileName);
emp_leaves.fileContent = new byte[emp_leaves.uploadFile.ContentLength];
emp_leaves.uploadFile.InputStream.Read(emp_leaves.fileContent, 0, emp_leaves.uploadFile.ContentLength);
Session["emp_leaves.uploadFile"] = emp_leaves.uploadFile; //saving the file in session variable here
}
else if (Session["emp_leaves.uploadFile"] != null)
{//if re-submitting after a failed validation you will reach here.
emp_leaves.uploadFile = (HttpPostedFileBase)Session["emp_leaves.uploadFile"];
if (emp_leaves.uploadFile != null && emp_leaves.uploadFile.ContentLength>0 && !string.IsNullOrEmpty(emp_leaves.uploadFile.FileName))
{
emp_leaves.fileNameOrig = Path.GetFileName(emp_leaves.uploadFile.FileName);
emp_leaves.uploadFile.InputStream.Position = 0;
emp_leaves.fileContent = new byte[emp_leaves.uploadFile.ContentLength];
emp_leaves.uploadFile.InputStream.Read(emp_leaves.fileContent, 0, emp_leaves.uploadFile.ContentLength);
}
}
//code to save follows here...
}
अंत भीतर मेरे विचार संपादित करें: यहाँ, मैं सशर्त हूँ फ़ाइल अपलोड नियंत्रण दिखा रहा है।
< script type = "text/javascript" >
$("#removefile").on("click", function(e) {
if (!confirm('Delete File?')) {
e.preventDefault();
return false;
}
$('#fileNameOrig').val('');
//toggle visibility for concerned div
$('#downloadlrfdiv').hide();
$('#uploadlrfdiv').show();
return false;
}); <
/script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
@model PPMSWEB.Models.emp_leaves @{ HttpPostedFileBase uploadFileSession = Session["emp_leaves.uploadFile"] == null ? null : (HttpPostedFileBase)Session["emp_leaves.uploadFile"]; } @using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data"
})) { @Html.AntiForgeryToken()
<div class="row">
@*irrelevant content removed*@
<div id="downloadlrfdiv" @((!String.IsNullOrEmpty(Model.fileNameOrig) && (Model.uploadFile==n ull || uploadFileSession !=null)) ? "" : "style=display:none;")>
<label>Attachment</label>
<span>
<strong>
<a id="downloadlrf" href="@(uploadFileSession != null? "" : Url.Action("DownloadLRF", "emp_leaves", new { empLeaveId = Model.ID }))" class="text-primary ui-button-text-icon-primary" title="Download attached file">
@Model.fileNameOrig
</a>
</strong>
@if (isEditable && !Model.readonlyMode)
{
@Html.Raw(" ");
<a id="removefile" class="btn text-danger lead">
<strong title="Delete File" class="glyphicon glyphicon-minus-sign"> </strong>
</a>
}
</span>
</div>
<div id="uploadlrfdiv" @(!(!String.IsNullOrEmpty(Model.fileNameOrig) && Model.uploadFile==n ull) && !Model.readonlyMode ? "" : "style=display:none;")>
<label>Upload File</label> @Html.TextBoxFor(model => model.uploadFile, new { @type = "file", @class = "btn btn-default", @title = "Upload file (max 300 KB)" }) @Html.ValidationMessageFor(x => x.uploadFile)
</div>
</div>
}
हाँ, मैं सुरक्षा सिद्धांत को समझता हूं। बस इच्छा है कि मैंने जो परिदृश्य वर्णित किया है, उसके लिए यह संभव था, क्योंकि केवल मेरे द्वारा उपयोगकर्ता अनुभव प्रभावित हुआ है। ;/ – Chaddeus
अपनी यूएक्स समस्या को ठीक करने के लिए कि उपयोगकर्ता को फ़ाइल को दोबारा चयन करना होगा: जब आप पहली बार सेट फ़ाइल के साथ फॉर्म सबमिट करते हैं तो आप सर्वर को एक अस्थायी कैश में फ़ाइल को स्टोर कर सकते हैं। फिर यदि सत्यापन विफल हो जाता है, तो आप फ़ील्ड सेट होने का संकेत देने के लिए फ़ाइल नाम (या थंबनेल) को सरल दिखाते हैं। सत्यापन पूर्ण होने के बाद, आप फ़ाइल कैश को अपने कैश से प्राप्त करते हैं। – tsauerwein
@tsauerwen, अच्छी तरह से एमवीसी स्टेटलेस मॉडल के उद्देश्य को हरा देता है। – Triynko