在asp.net中使用fileupload,如何将图像保存在文件夹中,然后调用并显示它?我可以通过Web方法使用Ajax,jquery或javascript吗?
$upn
user1@ourdomain.com
user2@ourdomain.com
user3@ourdomain.com
我在c#中有这些方法,但是我在javascript中需要它。
Foreach ($employee in $upn) {
Set-MailboxFolderPermission -AccessRights AvailabilityOnly *-Identity $employee* -User default
}
使用此方法:
<asp:FileUpload CssClass="image" ID="fileUpload" runat="server" />
答案 0 :(得分:0)
您可以使用通用处理程序上传文件。使用JQuery Ajax可以将文件上传到特定的文件夹。在提交按钮中,调用JavaScript函数
<input type="submit" value="Submit" onclick="return UploadFile();" />
jQuery Ajax
function UploadFile()
{
$.ajax({
url: "FileUploadHandler.ashx",
type: "POST",
data: data,
contentType: false,
async: false,
processData: false,
success: function (result) {
// Process the result
},
error: function (err) {
alert(err.statusText);
}
});
}
FileUploadHandler.ashx.cs
public class FileUploadHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
if (context.Request.Files.Count > 0)
{
HttpFileCollection files = context.Request.Files;
for (int i = 0; i < files.Count; i++)
{
HttpPostedFile file = files[i];
string extention = System.IO.Path.GetExtension(file.FileName);
string fileName = file.FileName.Replace(extention, "") + Guid.NewGuid() + extention;
string fname = context.Server.MapPath("~/FolderName/" + fileName);
file.SaveAs(fname);
context.Response.ContentType = "text/plain";
context.Response.Write(fileName);
}
}
}
}
有关更多信息,请检查此link
答案 1 :(得分:0)
您可以通过这种方式实现,并将文件保存在文件夹中
if (file != null)
{
if (file != null && file.ContentLength > 0)
{
string dirUrl = "~/Uploads/";
string dirPath = Server.MapPath(dirUrl);
if (!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
}
string fileUrl = dirUrl + "/" + Path.GetFileName(file.FileName);
file.SaveAs(Server.MapPath(fileUrl));
}
}
现在从文件夹或数据库中检索文件
if (Directory.Exists(Server.MapPath("~/Uploads/" + model.id+ "/")))
{
model.Image= Directory.EnumerateFiles(Server.MapPath("~/Uploads/" + model.id + "/"))
.Select(fn => "~/Uploads/" + model.id + "/" + Path.GetFileName(fn));
for(int i = 0; i < model.Image.Count();i++)
{
}
}
使用这样的ajax预览文件
if (file.type)
if (regeximage.test(file.name.toLowerCase())) {
var fileReader = new FileReader();
fileReader.onload = function (e) {
var file = e.target;
$("<span class=\"pip\">" +
"<div id=\"divimageId\">" + "<image id=\"imageid\" class=\"imageThumb\" frameborder=\'0\' src=\"" + e.target.result + "\" title=\"" + file.name + "\"></image>" + "</div>" +
"<br/><span class=\"remove\">Remove file</span>" +
"</span>").insertAfter("#divimageupload");
$(".remove").click(function () {
$(this).parent(".pip").remove();
$("#divimageupload").val('');
});
}
fileReader.readAsDataURL(file);
}