c# – 如何上传pdf文件?
发布时间:2020-12-16 00:01:34 所属栏目:百科 来源:网络整理
导读:我必须使用FileUpload控件在Web应用程序中上传.pdf文件.我试过这段代码,但它有一些问题.谁能帮我这个? protected void Button1_Click(object sender,EventArgs e) { if (FileUpload1.HasFile) { if (FileUpload1.PostedFile.ContentType == ".pdf") { strin
我必须使用FileUpload控件在Web应用程序中上传.pdf文件.我试过这段代码,但它有一些问题.谁能帮我这个?
protected void Button1_Click(object sender,EventArgs e) { if (FileUpload1.HasFile) { if (FileUpload1.PostedFile.ContentType == ".pdf") { string path = Server.MapPath(".") + "" + FileUpload1.FileName; FileUpload1.PostedFile.SaveAs(path); Label6.Text = "File Uploaded Successfully..."; StreamReader reader = new StreamReader(FileUpload1.FileContent); string text = reader.ReadToEnd(); } else Label6.Text = "Upload .pdf File"; } else Label6.Text = "Upload file"; } 解决方法
您应该重新构建代码,以便它可以准确地告诉您上载的错误.像这样的东西:
protected void Button1_Click(object sender,EventArgs e) { Label6.Text = ProcessUploadedFile(); } private string ProcessUploadedFile() { if(!FileUpload1.HasFile) return "You must select a valid file to upload."; if(FileUpload1.ContentLength == 0) return "You must select a non empty file to upload."; //As the input is external,always do case-insensitive comparison unless you actually care about the case. if(!FileUpload1.PostedFile.ContentType.Equals("application/pdf",StringComparison.OrdinalIgnoreCase)) return "Only PDF files are supported. Uploaded File Type: " + FileUpload1.PostedFile.ContentType; //rest of the code to actually process file. return "File uploaded successfully."; } 我的猜测是浏览器没有提供正确的内容/类型.尝试上面的代码并告诉我们您收到的消息. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- c – 来自std :: set insert()和find()的写入数据竞争?
- C 14 lambda的默认参数类型推导取决于前面的参数
- typeHandler接口实现FastJson中的JSONObject
- Cocos2d-x《雷电大战》(1)-双层地图无限滚动
- magento2 XML配置说明
- Cg Programming/ProgrammableGraphics Pipeline
- Oracle Job维护
- Fastjson是一个Java语言编写的高性能功能完善的JSON库。
- C++不使用变量求字符串长度strlen函数的实现方法
- Cocos2d-x使用Luajit将Lua脚本编译为bytecode,从而实现加密