File uploader for asp.net mvc
I have a project which upload multiple files so kindly if any one have the code of file upl开发者_如何学Gooad then please share the code.
I use the jquery MultiFile script.
After adding the jquery and multifile script references to your page, you would then add a file input to your form and set the class="multi"
<input type="file" width="50" id="fileUpload" class="multi" runat="server" />
On postback, I iterate through the uploaded files and save them to a directory on the server or the cloud or wherever.
for (int i = 0; i < Request.Files.Count; i++)
{
if (Request.Files[i].ContentLength > 0)
{
int retVal = saveFile((HttpPostedFile)Request.Files[i]);
if (retVal != -1)
documentID.Add(retVal); // documentID is a list of ID in my sql database. This code is located in a user control for document management.
}
}
// note I have a few utility methods here...
private int saveFile(HttpPostedFile file)
{
if (file.ContentLength > 0)
{
string directory = Server.MapPath(SiteRoot + UploadURL);
string directory_filename_uploaded = directory + file.FileName.Substring(file.FileName.LastIndexOf("\\") + 1);
// make sure its unique so we don't overwrite.
string directory_filename_unique = Document.GetUniqueFilename(directory_filename_uploaded);
string filename_unique = directory_filename_unique.Substring(directory_filename_unique.LastIndexOf("\\") + 1);
file.SaveAs(directory_filename_unique);
return insertFile(filename_unique);
}
else
return -1;
}
精彩评论