MVC-JS上传文件

<div class="file-loading">
    <input id="uploadify" name="uploadify" type="file" multiple>
</div>
<script>


    $(function () {
        $("#uploadify").fileinput({
            language: "zh",
            theme: "explorer",
            uploadUrl: '/ProjBaseManage/BidBudget/UploadifyFile?folderId=' + $.getUrlParam('folderId') + '&ProjectManageID=' + $.getUrlParam('ProjectManageID'),
            minFileCount: 1,
            overwriteInitial: false,
            hideThumbnailContent: true
        }).on('fileuploaded', function (event, data, previewId, index) {
            console.log(data);
        });
    })

</script>


[HttpPost]
        public ActionResult UploadifyFile(string folderId, string ProjectManageID)
        {
            try
            {
                var Filedata = Request.Files;


                Thread.Sleep(500);延迟500毫秒
                //没有文件上传,直接返回
                if (Filedata == null || Filedata.Count < 1 || string.IsNullOrEmpty(Filedata[0].FileName) || Filedata[0].ContentLength == 0)
                {
                    return HttpNotFound();
                }


                //获取文件完整文件名(包含绝对路径)
                //文件存放路径格式:/Resource/ResourceFile/{userId}{data}/{guid}.{后缀名}
                string userId = OperatorProvider.Provider.Current().UserId;
                string fileGuid = Guid.NewGuid().ToString();
                long filesize = Filedata[0].ContentLength;
                string FileEextension = Path.GetExtension(Filedata[0].FileName);
                string uploadDate = DateTime.Now.ToString("yyyyMMdd");
                string virtualPath = string.Format("~/Resource/BidBudget/{0}/{1}/{2}{3}", userId, uploadDate, fileGuid, FileEextension);
                string fullFileName = this.Server.MapPath(virtualPath);
                //创建文件夹
                string path = Path.GetDirectoryName(fullFileName);
                Directory.CreateDirectory(path);
                if (!System.IO.File.Exists(fullFileName))
                {
                    if (FileEextension.ToLower().IndexOf(".exe") > -1)
                    {
                        fullFileName = fullFileName.Remove(fullFileName.Length - 4) + ".ex_";
                    }
                    if (FileEextension.ToLower().IndexOf(".sys") > -1)
                    {
                        fullFileName = fullFileName.Remove(fullFileName.Length - 4) + ".sy_";
                    }
                    if (FileEextension.ToLower().IndexOf(".dll") > -1)
                    {
                        fullFileName = fullFileName.Remove(fullFileName.Length - 4) + ".dl_";
                    }
                    if (FileEextension.ToLower().IndexOf(".com") > -1)
                    {
                        fullFileName = fullFileName.Remove(fullFileName.Length - 4) + ".co_";
                    }
                    if (FileEextension.ToLower().IndexOf(".bat") > -1)
                    {
                        fullFileName = fullFileName.Remove(fullFileName.Length - 4) + ".ba_";
                    }
                    if (FileEextension.ToLower().IndexOf(".cmd") > -1)
                    {
                        fullFileName = fullFileName.Remove(fullFileName.Length - 4) + ".cm_";
                    }
                    if (FileEextension.ToLower().IndexOf(".bin") > -1)
                    {
                        fullFileName = fullFileName.Remove(fullFileName.Length - 4) + ".bi_";
                    }
                    if (FileEextension.ToLower().IndexOf(".js") > -1)
                    {
                        fullFileName = fullFileName.Remove(fullFileName.Length - 4) + ".j_";
                    }


                    //保存文件
                    Filedata[0].SaveAs(fullFileName);
                    //转换文件html
                    string htmlfiles = "pdf,doc,docx,ppt,pptx,xls,xlsx";
                    if (htmlfiles.IndexOf(FileEextension.Replace(".", "").ToLower()) > -1)
                    {
                        string tempfile = fullFileName.Replace("\\Resource\\BidBudget", "\\Resource\\TempFile");
                        //创建文件夹
                        string temppath = Path.GetDirectoryName(tempfile);
                        Directory.CreateDirectory(temppath);
                        AsposeToHtml aspose = new AsposeToHtml();
                        //excel--html
                        if (FileEextension.Equals(".xls") || FileEextension.Equals(".xlsx"))
                        {
                            aspose.ExcelToHtml(fullFileName, tempfile.Replace(".xlsx", ".html").Replace(".xls", ".html"));
                        }
                        //word--html
                        if (FileEextension.Equals(".doc") || FileEextension.Equals(".docx"))
                        {
                            aspose.WordToHtml(fullFileName, tempfile.Replace(".docx", ".html").Replace(".doc", ".html"));
                        }
                        //ppt--html
                        if (FileEextension.Equals(".ppt") || FileEextension.Equals(".pptx"))
                        {
                            aspose.PPTToHtml(fullFileName, tempfile.Replace(".pptx", ".html").Replace(".ppt", ".html"));
                        }
                        //pdf--html
                        if (FileEextension.Equals(".pdf"))
                        {
                            aspose.PdfToHtml(fullFileName, tempfile.Replace(".pdf", ".html"));
                        }
                    }


                    //文件信息写入数据库
                    BidBudgetFileInfoEntity fileInfoEntity = new BidBudgetFileInfoEntity();
                    fileInfoEntity.Create();
                    fileInfoEntity.FileId = fileGuid;
                    if (!string.IsNullOrEmpty(folderId))
                    {
                        fileInfoEntity.FolderId = folderId;
                    }
                    else
                    {
                        fileInfoEntity.FolderId = "0";
                    }
                    fileInfoEntity.ProjectManageID = ProjectManageID;
                    fileInfoEntity.FileName = Filedata[0].FileName;
                    fileInfoEntity.FilePath = virtualPath;
                    fileInfoEntity.FileSize = filesize.ToString();
                    fileInfoEntity.FileExtensions = FileEextension;
                    fileInfoEntity.FileType = FileEextension.Replace(".", "");
                    fileInfoBLL.SaveForm("", fileInfoEntity);
                }
                return Success("上传成功。");
            }
            catch (Exception ex)
            {
                return Content(ex.Message);
            }
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 以下是一个简单的 spring-mvc.xml 文件示例: ``` <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.example.controller"/> <mvc:annotation-driven /> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans> ``` 请注意,上面的文件只是一个示例,您可以根据自己的需求进行修改和扩展。 ### 回答2: 生成一个简单的spring-mvc.xml文件需要以下几个步骤: 1. 首先,在文件的开头添加xml的声明和命名空间。声明如下: ``` <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> ``` 2. 接下来,配置mvc的注解驱动。在文件中添加如下代码: ``` <mvc:annotation-driven /> ``` 这样spring将会自动注册默认的一些处理器和适配器,用来支持基于注解的请求映射和请求参数的解析和绑定。 3. 然后,配置视图解析器,用来解析Controller方法返回的视图名。可以通过以下代码添加一个InternalResourceViewResolver视图解析器: ``` <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> ``` 上述配置将会将Controller方法返回的视图名加上前缀"/WEB-INF/views/"和后缀".jsp",最终形成完整的JSP视图路径。 4. 配置组件扫描,以便自动扫描和注册Controller,Service,Repository等组件。可以通过以下代码配置组件扫描: ``` <context:component-scan base-package="com.example.controller" /> <context:component-scan base-package="com.example.service" /> <context:component-scan base-package="com.example.repository" /> ``` 上述代码将分别扫描并注册com.example.controller,com.example.service和com.example.repository下的组件。 5. 最后,添加xml文件的结尾标签 "</beans>" 结束配置。 通过以上步骤,就能生成一个简单的spring-mvc.xml文件,用于配置Spring MVC的基本设置和组件的注册。 ### 回答3: 这是一个简单的spring-mvc.xml文件的示例: ``` <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <mvc:annotation-driven/> <mvc:resources mapping="/resources/**" location="/resources/"/> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basename" value="messages"/> </bean> <bean id="myController" class="com.example.MyController"></bean> <bean id="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> <bean id="viewController" class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="10000000"/> </bean> </beans> ``` 这个配置文件中,主要包含以下配置: 1. `<mvc:annotation-driven/>` 这个配置用于启用Spring MVC的注解驱动。 2. `<mvc:resources>` 用于配置静态资源的处理器,比如js、css等文件。 3. `<bean id="viewResolver">` 配置了一个InternalResourceViewResolver视图解析器,用于将逻辑视图名对应的JSP文件解析为实际的物理视图。 4. `<bean id="messageSource">` 配置了一个ResourceBundleMessageSource用于国际化处理,它可以加载不同语言的消息资源文件。 5. `<bean id="handlerMapping">` 配置了一个BeanNameUrlHandlerMapping,用于将请求的URL映射到对应的处理器。 6. `<bean id="viewController">` 配置了一个UrlFilenameViewController,用于将URL的文件名作为逻辑视图名进行解析。 7. `<bean id="multipartResolver">` 配置了一个CommonsMultipartResolver,用于处理文件上传的请求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值